【问题标题】:use curl to download indirect image file使用 curl 下载间接图像文件
【发布时间】:2009-12-14 03:11:24
【问题描述】:

我尝试从以下代码的 url 下载图像文件,但它没有从服务器返回正确的内容。图像可以通过加载 url 在浏览器中呈现,或者在 shell 模式下使用 curl 下载,但不能在 php 执行中。在 php 执行中,从服务器返回的标头的内容类型是 '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);

【问题讨论】:

  • 我刚刚测试了你的代码,图片下载正确。
  • 图片文件能否正确打开。在我的mac上,下载的文件大小只有20字节,不是原来的大小。
  • 我打开了它,就像我在浏览器上打开它一样。也许它是某种 curl 配置问题。
  • 确保您已启用 allow_url_fopen

标签: php image curl download


【解决方案1】:

为什么不直接使用 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);

【讨论】:

  • 这只有在你的服务器允许 url_fopen 时才有效(我认为这是 ini 指令的名称)。
  • 配置名称是allow_url_fopen :)
  • 启用allow_url_fopen不会是安全问题!!?
猜你喜欢
  • 2015-11-26
  • 2011-06-30
  • 2014-04-06
  • 2016-05-02
  • 2012-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多