簡體   English   中英

如何使用PHP訪問Facebook用戶的個人資料圖片?

[英]How do I access the profile picture of a facebook user in PHP?

我正在開發一個將使用用戶個人資料圖片的應用程序。 我試圖將用戶的個人資料圖片保存在我的服務器上以進行操作。

當我使用https://graph.facebook.com/[uid]/picture?type=large ,它將無法正常工作,因為在提供圖片之前,facebook會進行某種重定向

誰能幫我解決這個問題? 我正在使用PHP。

您可以使用圖形API檢索用戶個人資料圖片的直接URL
https://graph.facebook.com/{user_id}/picture

<?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來獲取它。

如果我正確理解了您的問題,您可能正在使用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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM