以PHP格式保存来自远程服务器的图像
我需要保存来自远程站点的图像。我的主机不允许file_get_contents,所以我想用curl。我正在使用代码获取corrput图像。请帮忙!以PHP格式保存来自远程服务器的图像
$destination = realpath("../../app/webroot/img/uploads") . "/" . $facebook_id . "." . "gif";
// Delete previous pic
if (file_exists($destination)) {
unlink($destination);
}
// Save new pic
$remoteUrl = "https://graph.facebook.com/" . $facebook_id . "/picture";
$ch = curl_init($remoteUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$rawdata = curl_exec($ch);
curl_close($ch);
$fp = fopen($destination, 'w');
fwrite($fp, $rawdata);
fclose($fp);
$fp = fopen($destination, 'wb');
相反,打开它们作为二进制文件! :)
用wb试过,当我打开图像时显示'图像无法打开它包含错误' – user1871640 2013-03-13 21:49:33
设置CURLOPT_READFUNCTION或CURLOPT_WRITEDATA也许? – 2013-03-13 21:53:14
看来您的服务器正在将graph.facebook.com解析为由于某种原因未连接的IPv6地址。如果你在PHP5.3 +,你可以尝试强制卷曲使用IPv4:
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
+1也在考虑这一点,根据OP发布的错误 – thaJeztah 2013-03-15 22:29:54
你看着这个被破坏的数据?这是一个局部图像? 0长度的文件?你有没有检查'$ rawdata'是否真的是一个布尔错误来表示curl以某种方式失败? – 2013-03-13 21:44:57
从未与Facebook合作过,是否有可能阻止你如何? – 2013-03-13 21:51:33
@MarcB $ rawdata为空 – user1871640 2013-03-13 21:53:00