简体   繁体   中英

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

I am developing an app that will use the profile picture of user. I am trying to save the profile picture of the user on my server for manipulation.

When I use https://graph.facebook.com/[uid]/picture?type=large it will not work because there is some sort of redirection that facebook does before serving the pic.

Can anyone help me with this issue? I am using 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 '" />';          

?>

Now, if what you want is to save the image to the database then see this

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

Then when storing the image use that $userpicture to get it.

If I understand your issue correctly, you are probably using file_get_contents to fetch the image but you are just getting a text response back, because file_get_contents does not automatically follow the redirect the text response contains?

A quick-and-dirty solution is to parse the text response yourself to find the url of the actual image:

@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);

Then you should have the actual image url that you can process as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM