我如何在PHP中访问facebook用户的个人资料图片


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

我正在开发一个应用程序,将使用用户的个人资料图片。我正试图将用户的个人资料图片保存在我的服务器上以供操作。

当我使用https://graph.facebook.com/[uid]/picture?type=large时,它将不起作用,因为有某种重定向, facebook在服务pic之前做。

有人能帮我解决这个问题吗?

如果我正确理解您的问题,您可能正在使用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,你可以根据需要处理

您可以使用图形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来获取它。