php - 使用curl下载间接图像文件

标签 php image curl download

我尝试从以下代码的 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/

相关文章:

php mysql_query 使用变量作为字段名称

php - 在 mysql 数据库中搜索结果包含部分匹配的字符串

php - Cron Job 使用 php 脚本发送电子邮件时出错

ImageMagick/Mogrify - 以编程方式压缩图像

post - cURL POST --data-binary vs --form

ssl - cURL:尝试在 RHEL 7 上安装 kubectl 时出现 SSL 连接错误

android - Gradle执行curl不上传文件

php - 如何从联接表中选择列 : laravel eloquent

ruby-on-rails - 渲染目录中的图像

html - 图片链接无法跨浏览器工作,已检查以确保 div 未被覆盖 - 我错过了什么?