我尝试从以下代码的 url 下载图像文件,但它没有从服务器返回正确的内容。图像可以通过加载 url 在浏览器中呈现,也可以在 shell 模式下使用curl 下载,但不能在 php 执行中呈现。在 php 执行中,从服务器返回的 header 内容类型是“text/html
”,而不是它应该是的“image/jpeg
”。
有人对此有什么想法吗?
$url = 'http://count.koubei.com/showphone/showphone.php?f=jpg&w=104&h=10&bc=255,255,255&fc=0,0,0&fs=10&fn=arial&phone=LTIxMDM3MjIyOTc%3D%23dWBzmKEZpTh7YcWvOw%3D%3D';
$file_handler = fopen('phone.jpeg', 'w');
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_FILE, $file_handler);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_exec($curl);
curl_close($curl);
fclose($file_handler);
最佳答案
为什么不直接使用 file_get_contents 像这样
$img = file_get_contents("http://count.koubei.com/showphone/showphone.php?f=jpg&w=104&h=10&bc=255,255,255&fc=0,0,0&fs=10&fn=arial&phone=LTIxMDM3MjIyOTc%3D%23dWBzmKEZpTh7YcWvOw%3D%3D");
file_put_contents("photo.jpg",$img);
关于php - 使用curl下载间接图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1898711/