2011-12-03 49 views
0

我正在開發一個應用程序,將使用用戶的個人資料圖片。我正在嘗試將用戶的配置文件圖片保存在我的服務器上進行操作。如何在PHP中訪問Facebook用戶的個人資料圖片?

當我使用https://graph.facebook.com/[uid]/picture?type=large它不會工作,因爲有某種重定向 Facebook在服務圖片之前做的。

任何人都可以幫助我解決這個問題嗎?我正在使用PHP。

+0

你用什麼來下載圖像?嘗試修改它以遵循重定向。 – abraham

+0

顯示您用來調用該URL的PHP​​。您是否使用Facebook API框架? – HerrSerker

回答

0

如果我正確理解你的問題,你可能使用file_get_contents來獲取圖像,但你只是得到一個文本響應回來,因爲file_get_contents不會自動遵循文本響應包含的重定向?

快速和骯髒的解決辦法是你自己分析文本響應找到實際的圖像的URL:

@file_get_contents('https://graph.facebook.com/'.$userid.'/picture?type=large'); 
foreach ($http_response_header as $rh) if (substr($rh, 0, 10)=='Location: ') $imgurl=substr($rh, 10); 

那麼你應該有實際的圖像URL根據需要,你可以處理。

0
<?php 
# You can do: 

$userid = SOMEID; 

$piclink = 'https://graph.facebook.com/'.$userid.'/picture?type='; 

#then a option to choose the type, 
#there is the small, normal, square and large type. 
#So lets say you choose the large size: 

$pictype = 'large'; 

$userpicture = $piclink . pictype; 

# ---------------------------------- 
# Now that we have a picture link you can do various stuff like: 

echo '<img src="'; 
echo $userpicture; # This will output the updated user picture with no problem 
echo '" />';   

?> 

現在,如果你想要的是將圖像保存到數據庫,然後看到這個

http://www.phpriot.com/articles/images-in-mysql

然後存儲圖像使用$ userpicture得到它的時候。

相關問題