我需要保存远程站点的图像。我的主机不允许file_get_contents,所以我尝试使用curl。我正在使用代码获取corrput图像。请帮忙!
$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);
答案 0 :(得分:1)
$fp = fopen($destination, 'wb');
相反,打开它们作为二进制文件! :)
答案 1 :(得分:1)
您的服务器似乎正在将graph.facebook.com解析为由于某种原因未连接的IPv6地址。如果您使用的是php5.3 +,可以尝试强制curl使用IPv4:
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);