cURL脚本不会下载图片;相反渲染垃圾 [英] cURL script will not download images; Instead renders junk

查看:137
本文介绍了cURL脚本不会下载图片;相反渲染垃圾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用以下功能从经销商下载照片,在我们的网站上使用,如此处

I have been using the following function to download pictures from a distributor for use on our website as was described here:

$url = "http://covers.stl-distribution.com/7819/lg-9781936417445.jpg";
$itemnum = 80848;
$path = "www.gullions.com/localstore/test/test.jpg";
header('Content-type: image/jpeg');
$ch = curl_init($url); 
$fp = fopen("http://www.gullions.com/localstore/test/test.jpg", 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);

而不是下载和保存图片,它只打印疯狂的字符到屏幕。我知道这意味着,由于某种原因,浏览器正在尝试渲染图片,但很可能无法识别文件类型。但我似乎找不到问题。您可以通过在此处浏览来查看疯狂的输出。要验证该图片是否未下载和保存,您可以在此处浏览。此外,FTP还显示没有下载任何文件。如果您导航到原始图片的下载网址,您会看到

Instead of downloading and saving the picture, it only prints crazy characters to the screen. I know that means that for some reason the browser is trying to render the picture but most likely doesn't recognize the file type. But I can't seem to find the problem. You can see the crazy output by navigating here. To verify that the image wasn't downloaded and saved, you can navigate here. Also, FTP also shows that no file was downloaded. If you navigate to the original picture's download url you'll see that the file we are trying to download does in fact exist.

我已联系我的主机,并确认没有更改服务器的设置,cURL正常工作,以及甚至还回滚了我的浏览器,以验证最近的更新没有导致该问题。我创建了一个测试文件,通过从应用程序中删除函数,并尝试单独运行它,但只有相同的结果。

I have contacted my host and verified that no settings have been changed with the server, that cURL is functioning properly, and have even rolled back my browser to verify that a recent update didn't cause the issue. I created a test file by removing the function from the application and have tried running it separately but have only had the same results.

推荐答案

添加:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

默认情况下,此参数为 false 文档阅读

as by default this parameter is false. Docs read:


TRUE将传回作为
curl_exec()的返回值的字符串返回,而不是直接输出。

TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

编辑

设置 CURLOPT_RETURNTRANSFER code> curl_exec() 返回数据,因此应该手动编写,如下所示:

Setting CURLOPT_RETURNTRANSFER make curl_exec() return data, so it should be written manually, like this:

$url = "http://covers.stl-distribution.com/7819/lg-9781936417445.jpg";
$ch = curl_init($url);
$fp = fopen("./test.jpg", 'wb');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HEADER, 0);
fwrite($fp, curl_exec($ch));
curl_close($ch);
fclose($fp);

此代码使用 CURLOPT_FILE 对我来说很好:

Also this code, that uses CURLOPT_FILE works for me just fine:

$url = "http://covers.stl-distribution.com/7819/lg-9781936417445.jpg";
$ch = curl_init($url);
$fp = fopen("./test.jpg", 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

所以我基本上怀疑你的文件句柄是无效的,因此cURL回到默认输出。尝试此代码,并进行基本错误检查(您应始终检查错误):

so I basically suspect that your file handle is not valid, therefore cURL falls back to default output. Try this code with elementary error checking (you should ALWAYS check for errors):

$url = "http://covers.stl-distribution.com/7819/lg-9781936417445.jpg";
$fp = fopen("./test.jpg", 'wb');
if( $fp != null ) {
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_FILE, $fp);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_exec($ch);
  curl_close($ch);
  fclose($fp);
} else {
  exit('ERROR: Failed to open file');
}

请注意,我的示例写入相同的文件夹脚本。除非你的服务器得到文件权限坏了。如果它适用于您,请调查(通常)您的脚本以运行的用户是否可以写入您的目标文件夹。

Note that my examples write to the same folder scripts sits in. It must work unless your server got file permissions messed badly. If it works for you, then investigate if (usually) user your scripts runs as can write to your target folder.

这篇关于cURL脚本不会下载图片;相反渲染垃圾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆