繁体   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