2009-12-14 54 views
2

我尝试从以下代码中从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); 
+0

我只是测试你的代码,正确下载图像。 – Juan 2009-12-14 03:19:28

+0

可以正确打开图像文件。在我的Mac上,文件的下载大小只有20个字节,这不是原始大小。 – Katat 2009-12-14 03:35:58

+0

我打开它,就好像我在浏览器上打开它一样。也许它是某种卷曲配置问题。 – Juan 2009-12-14 03:41:25

回答

5

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

这简单得多! – Katat 2009-12-14 22:36:06

+2

这只有在你的服务器允许'url_fopen'(我认为这是ini指令的名称)时才有效。 – 2010-08-02 21:29:15

+1

配置名称是'allow_url_fopen' :) – Nick 2011-04-25 00:34:21