Can't get Facebook's user photos with php - facebook

I'm trying to get the user photos, but I cannot get the user's photos data.
I'm trying this:
$photos = $facebook->api('https://graph.facebook.com/'.$profileFID.'/photos?access_token='.$access_token);
print_r($photos);
I'm sure the URL is correct since pasting it in the browser shows the photos array. However when I try to print the array to check if I got it I get only this:
( [id] => https://graph.facebook.com/1409006586/photos )
How can I get the full array with the user photos?

The Facebook SDK will take care of constructing the url for you (and append the access_token), you just need to build the query:
$photos = $facebook->api('me/photos');
If the user is not currently logged in but you have the access_token, then use:
$photos = $facebook->api($profileFID.'/photos', 'get', array('access_token'=>$access_token));
P.S: always put your queries in a try...catch blocks

https://graph.facebook.com/1409006586/photos
replace photo by picture
ex
https://graph.facebook.com/4/picture

Related

Facebook Graph API only returning 15 photos with no pagination options

I'm trying to download all of the photos that I've been tagged in from facebook.
https://graph.facebook.com/me/photos?access_token=blah
Returns a data[] with 15 pictures and a paging object:
'paging':
{
'cursors':
{
'after': 'afterHash',
'before': 'beforeHash'
}
}
I expected to see next and previous urls in paging to get at more photos.
Calling: https://graph.facebook.com/me/photos?access_token=blah&type=tagged&after=afterHash returns an empty data[]
Adding &limit=14 to the request returns 14 pictures and the next url that I would expect. Getting that next url only returns 1 picture though, for a total of 15 again.
Further details on the pictures being returned by the api call:
Created Times range from 2013-08-25 to 2015-10-10
5 photos uploaded by friends and tagged, 10 uploaded by myself.
When I look at "/Photos/Photos of You" from my profile page I see many more pictures than are being returned by the API (50+ for 2015, hundreds in years before that). The 15 that are being returned are a mixture of pictures that I have uploaded and those uploaded by friends who then tagged me.
By default reading from the photos edge includes all photos a person has been tagged in.
Required Permission : user_photos
Referance: https://developers.facebook.com/docs/graph-api/reference/user/photos
You can make following API Call. Because no one tagged in 999,999,999,999,999 Photos.
https://graph.facebook.com/me/photos?fields=images{source}&limit=999999999999999&access_token=xxxxx
You can retrieve all photos which was allowed on Timeline. You can see those from here:
https://www.facebook.com/your_username_or_id/photos_of
Here is the sample php code which will get source for all photos using curl:
<?php
$curl = curl_init();
curl_setopt_array($curl,array(
CURLOPT_RETURNTRANSFER =>1,
CURLOPT_URL => 'https://graph.facebook.com/me/photos?fields=images{source}&limit=999999999999999&access_token=xxxxx',
CURLOPT_SSL_VERIFYPEER => false
));
$array = json_decode(curl_exec($curl),true);
curl_close($curl);
for( $i = 0; $i<count($array['data']);$i++)
{
echo $array['data'][$i]['images'][0]['source']."</br>";
}
?>
First of all was your access token requested with the user_photos permission?
Also, as taken from https://developers.facebook.com/docs/graph-api/reference/photo:
A user access token may read a photo that user is tagged in if they
have granted the user_photos or user_posts permission. However, in
some cases the photo's owner's privacy settings may not allow your
application to access it.
Most likely though, you need to ask for all albums (via https://graph.facebook.com/me/albums?access_token=blah) and then in turn ask each of these for all the photos. The 15 photos you are retrieving are the photos from the first ever album created as mentioned in Get all photos of a page using Facebook API
Try it out here: https://developers.facebook.com/tools/explorer/?method=GET&path=me%2Falbums&version=v2.5

getting facebook pages liked by a friend

I'm using Django-social-auth for facebook login and Facepy to retrieve data from facebook.
Now I'm trying to get the list of all the pages liked by a user's friend. If I use:
graph.get('some_friend_id/likes')
I get an empty list of likes ( {'data': []} ).
I have added 'friends_likes' on my facebook app's User & Friend Permissions, and still it doesn't work.
my code looks like this:
instance = UserSocialAuth.objects.filter(user=request.user).filter(provider='facebook')
graph = GraphAPI(instance[0].extra_data['access_token'])
ps = graph.get('me/friends')
myfriends = ps['data']
frnd_id = myfriends[29]['id'] # getting a friend's ID
lk = frnd_id + '/likes'
frnd_likes = graph.get(lk)
print frnd_likes
A little help would be great. Thanks!

Using facebook SDK for php for an FQL query?

I would like to know how to use the following fql query to get a single message from my facebook inbox using facebook sdk for php (or another way if it's possible )
https://graph.facebook.com//fql?q=SELECT+body+FROM+message+WHERE++mssage_id="XXXXXXXXXXXXXXXXX_XXX"&access_token=...
(returns a JSON result)
OR
https://api.facebook.com/method/fql.query?query=SELECT+body+FROM+message+WHERE+message_id="XXXXXXXXXXXXXXXXX_XXX"&access_token=...
(returns an XML result)
both methods need an access token for a read_mailbox permission. All i need is to get the result using php. Any help ?
Thanks.
The most flexible way is to use the api method of the PHP SDK. The Facebook documentation site has examples of this.
If you already have an access token, this code will get you the data into a php array:
<?php
$query = 'SELECT body FROM message WHERE message_id = "?????????????_??"';
$token = '????';
$result = json_decode( file_get_contents('https://graph.facebook.com/fql?q=' .
urlencode($query) . '&access_token=' . $token) );
printf('<pre>%s</pre>', print_r($result, true) );
Yes you need an access token for this. If you're trying to read the mailbox of a random user, you need an app, and you'll have to follow one of the authentication workflows to get them to log into your app and give you the read_mailbox permission.

How to get facebook profile picture with 'F'-logo?

I use Graph API and I need to get users' profiles pictures by their UIDs.
It's easy to do: http://graph.facebook.com/[Facebook UID]/picture
I remember, some time ago, there was a litte Facebook Logo (litter 'f'-char) on these profile pictures. But now, I get only profile pictures without this logo.
How can I get images with this 'f'-logo?
Facebook doesn't store images with the 'f' on, so it's a little hacky to get at them.
I've found that this works:
You'll need the final image url, after graph.facebook.com/[id]/picture redirects.
http://external.ak.fbcdn.net/safe_image.php?&url=[imageurl]&logo
eg. me
I think it's not possible using the graph api.
It was possible using FQL (dont know if it is still working):
SELECT pic_with_logo FROM user WHERE uid = "..."
or use xfbml with <fb:profile-pic facebook-logo="true">
With jQuery and JS API you can make an FQL call:
FB.api({ method: 'fql.query', query: 'SELECT uid, pic_with_logo FROM user WHERE uid = me()' }, function(response) {
var user = response[0];
$('#container').append('<img src="'+user.pic_with_logo+'"/><br>');
});
also you can try:
pic_small_with_logo, pic_big_with_logo, pic_square_with_logo and the pic_with_logo i use in example.
here is the user table:
https://developers.facebook.com/docs/reference/fql/user/
Based on Tom's answer, Here is php example to get user's
$_json = json_decode(file_get_contents('http://graph.facebook.com/[FACEBOOK_UID]?fields=picture'));
if (isset($_json->picture)) {
header("Location: http://external.ak.fbcdn.net/safe_image.php?&url=" . $_json->picture . "&logo");
exit;
}
Here is what we do:
Query picture url via opengraph
Get the picture url from json object
redirect the page to facebook image handler with addition &logo to url

FQL does not return post with customized location

i wrote a small FQL script that returns the latest wallposts of my fanpage. The script works fine. Until today.
The situation:
I published a wallpost with custom settings: The post is only visible to German fans.
The Problem:
My fql query ignores this "special" post.
My little PHP program looks like this
$query = " SELECT
post_id, message, created_time, attachment,action_links, privacy, type
FROM
stream
WHERE
source_id = ".$page_id."
AND actor_id = ".$page_id."
ORDER BY created_time DESC";
$param = array(
'method' => 'fql.query',
'query' => $query,
'callback' => ''
);
$result = $this->facebook->opengraph->api($param);
The nugget:
Facebook returns the status message if i call the special post like this:
https://graph.facebook.com/367501354973
(Source: http://developers.facebook.com/docs/ref … api/status)
Thanks for helping
In general, content which is demographically targeted or restricted will only be made available to a user access token for a user who meets the demographic set - check that the user you're trying to access the posts as is actually in the demographic you targeted the post at.
I got a similar problem using the grpah api:
This does NOT return the special post
https://graph.facebook.com/MYPAGE/posts?access_token=SOME_TOKEN
If I call the graph via FB-Docs (they offer a special access token, i get the special post
https://graph.facebook.com/MYPAGE/posts?access_token=22223423470867|2.qnqX1HsEaIXVr0lVHCRxiw__.3600.12913423423400-10043534534530485|4wqk8YYWXQ8bjfQj8KETz2JyWQk
Stange...
If your FQL is calling without an access_token, then the API won't return any posts that any kind of restrictions placed on them. In your example, geo gated to Germany.
The reason it's working via the Docs is that we're appending an access_token.