使用curl、fileget_contents、fopen/frad/fwrite、copy-奇怪的标题保存facebo


save facebook image using curl, file_get_contents, fopen/fread/fwrite, copy - strange headers

我试图从facebook保存图像,但遇到了问题。

除了来自域external.ak.fbcdn.net 的图像外,它运行良好

即。http://external.ak.fbcdn.net/safe_image.php?d=85623fa1fc7679d3e4e5e9ab3b9ad426&w=50&h=50&url=http%3A%2F%2Upload.wikimedia.org%2Fwikipedia%2Fcommons%2F3%2F38%2FTwo_dangers.jpg&作物

使用curl、file_get_contents、fopen/frad/fwrite,复制返回1x1 gif,而不是实际图像。

我找不到解决方案,所以我检查了上面发布的url上的标题(get_headers),并得到了gif:

array(10) {
  [0]=>
  string(15) "HTTP/1.0 200 OK"
  ["Content-Type"]=>
  string(9) "image/gif"
  ["Pragma"]=>
  string(8) "no-cache"
  ["X-FB-Debug"]=>
  string(44) "qcXPNe3kW3TLOCgBRQQUBr/ekWiKISQTqbdUbDi7vXs="
  ["X-Cnection"]=>
  string(5) "close"
  ["Content-Length"]=>
  string(2) "43"
  ["Cache-Control"]=>
  string(44) "private, no-cache, no-store, must-revalidate"
  ["Expires"]=>
  string(29) "Sat, 31 Mar 2012 20:52:23 GMT"
  ["Date"]=>
  string(29) "Sat, 31 Mar 2012 20:52:23 GMT"
  ["Connection"]=>
  string(5) "close"
}    

有人能帮我做这个吗?这是某种脸书保护还是我做错了什么?

以防万一,这里是我的卷曲代码:

$ch = curl_init( $url );
$fp = fopen( $filePath, 'wb');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

对于前缀为.../safe_image.php的URL,您需要提取图像的真实URL:

$url = 'http://external.ak.fbcdn.net/safe_image.php?d=85623fa1fc7679d3e4e5e9ab3b9ad426&w=50&h=50&url=http%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2F3%2F38%2FTwo_dancers.jpg&crop';
$parts = parse_url($url);
parse_str($parts['query'], $params);
$realUrl = $params['url'];
print_r($realUrl);
// http://upload.wikimedia.org/wikipedia/commons/3/38/Two_dancers.jpg