douyan8070 2011-04-23 10:19
浏览 111
已采纳

如何在php curl函数中获取响应的mime类型

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?

  • 写回答

1条回答 默认 最新

  • douyanjing0822 2011-04-23 10:40
    关注

    The content type can be found out by this statement:

    echo curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    

    and you are already using it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件