简体   繁体   中英

how to get the mime type of the response in php curl function

I am using PHP CURL to fetch data from a server. The response can be any thing from a binary file to a JSON response, and depending upon the response i want to save the file like if a .pdf file then in pdf folder else if a JSON response, then fetch the data and process the data as given. the code that i am using

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
// create a file
$fp = fopen('my_test.pdf', 'w');
// write the contents of file 
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
// get the content type
echo curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch); 

the above code works fine, but i am not able to figure out what will be the response from the server, it can be any thing from .pdf,.doc,.jpg or a JSON response. Depending upont the response i will have process. How can i get the response type?

The content type can be found out by this statement:

echo curl_getinfo($ch, CURLINFO_CONTENT_TYPE);

and you are already using it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM