2012-03-31 20 views
1

我試圖保存來自Facebook的圖像並出現問題。使用curl保存facebook圖像,file_get_contents,fopen/fread/fwrite,複製 - 奇怪的標題

它運作良好,除了圖片來自域external.ak.fbcdn.net

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

使用curl,的file_get_contents,FOPEN/FREAD/FWRITE,複製迴歸的1x1 GIF圖片,而不是實際的圖像。

我coudn't找到解決辦法,所以我查了一下上面的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" 
}  

是否有人可以幫助我?這是一種Facebook保護或我做錯了什麼?

以防萬一,這裏是我的捲曲代碼:

$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); 
+0

[保存的facebook可能重複使用cURL](http://facebook.stackoverflow.com/questions/9680676/save-facebook-profile-image-using-curl) – ifaour 2012-03-31 21:26:31

+0

acualy no - 我正確保存配置文件圖像並將FOLLOWLOCATION設置爲true – John 2012-03-31 21:35:50

回答

2

對於URL的與.../safe_image.php前綴,你需要提取圖像的真實網址:

$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 
+0

謝謝你的回答! – John 2012-04-01 09:52:51

+0

@John,歡迎來到[so],[接受答案](http://meta.stackexchange.com/a/65088/176186)如果它可以幫助你解決問題;) – 2012-04-01 09:56:29

+0

它總是這樣嗎?我的意思是它的作品,但這些是兩個不同的圖像(第一個是50x50和第二個633x800 – John 2012-04-01 10:00:14