[英]Saving and image to server from a URL using cURL results in a blank image
$camera_ip = "10.10.10.10";
$image_url = "http://$camera_ip/cgi-bin/image.jpg?camera=right&size=1024x768&quality=60";
$ch = curl_init($image_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$rawdata = curl_exec($ch);
curl_close($ch);
$fp = fopen('./logo.jpg', 'wb');
fwrite($fp, $rawdata);
fclose($fp);
A file is being saved at 7KB
but the size is 0x0
... I can't figure out why the file isn't being saved correctly. 正在将文件保存为
7KB
但大小为0x0
...我无法弄清楚为什么未正确保存文件。 My guess is that the file isn't being loaded fully before being saved, but is there a way of ensuring that? 我的猜测是文件在保存之前没有完全加载,但是有没有办法确保?
Try using "w" instead of "wb" $fp = fopen('./logo.jpg', 'w');
尝试使用“ w”代替“ wb”。
$fp = fopen('./logo.jpg', 'w');
Or try: file_put_contents('./logo.jpg',$rawdata);
或者尝试:
file_put_contents('./logo.jpg',$rawdata);
(not sure it is binary safe, should be) (不确定它是二进制安全的,应该是)
And 和
Check your curl result to see what came back. 检查您的卷曲结果,看看结果如何。
$info = var_export(curl_getinfo($ch),true);
file_put_contents('log.txt',$info);
OR 要么
header('content-type: text/plain');
var_export(curl_getinfo($ch));
The curl Request failed (returned false). curl请求失败(返回false)。
Now you need to find out WHY. 现在,您需要找出原因。
For now change your: 现在,更改您的:
curl_setopt($ch, CURLOPT_HEADER, 0);
To: 至:
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
Add these options for trouble shooting: 添加以下选项以解决问题:
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$data = curl_exec($ch);
if (curl_errno($ch)){
echo ' Retrieve Base Page Error: ' . curl_error($ch);
}
else {
$info = rawurldecode(var_export(curl_getinfo($ch),true));
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$responseHeader= substr($data,0,$skip);
$data= substr($data,$skip);
echo "HEADER: $responseHeader\n";
echo "\n\nINFO: $info\n\nDATA: $data";
}
You will likely get the "Retrieve Base Page Error:" but it will also tell WHY it failed. 您可能会收到“检索基本页面错误:”,但它还会告诉您为什么失败。 If you cannot echo, replace
echo
with $info .=
如果无法回显,请用
$info .=
替换echo
$info .=
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.