Get a photo with most likes in facebook album - php

How can i get the photo with the most likes in a facebook album and when i get for a single album how can i count for example for last 7 created albums the picture with the most likes?

There's no option to sort Graph API results as far as I know. So, either you count and sort the results of the Graph API request
GET /{album_id}/photos?fields=album,id,likes
yourself (see https://developers.facebook.com/docs/graph-api/reference/v2.1/album/photos#read), or if you have a Graph API v2.0 app, you can use the following FQL to retrieve the most liked photo of an album:
select object_id, like_info.like_count from photo where album_object_id="{album_id}" order by like_info.like_count desc limit 0, 1
where {album_id} is your actual album_id.
Concerning the 7 last created albums, you'd have to query first for the most recent albums, and then execute the above request for each of them (because FQL doesn't support GROUP BY statements). You can use the Batch API to run these 7 requests in parallel (see https://developers.facebook.com/docs/graph-api/making-multiple-requests).
If you put some effort in the Batch call generation, you could even do the whole 8 requests in one, if you use the referencing feature. Have a look at https://developers.facebook.com/docs/graph-api/making-multiple-requests#operations

Related

FB Graph Api Search, convert FQL query to fetch nearby pages?

I've used the old Facebook SDK where I could do FQL-queries. I want to fetch all nearby places that also has a page attached. And with those results, I want as many columns as possible.
I want to rewrite this function so I can do the same but with the latest Facebook SDK 2.3.
The old way:
SELECT name, type, page_id, hours, categories, about, bio, description, general_info, location, checkins, fan_count, phone, pic_big, website FROM page WHERE page_id IN(SELECT page_id FROM place WHERE distance(latitude, longitude, '37.76', '-122.427') < 1000)
But this doesnt work so well with the new SDK. I've tried the search-method and tried to write different types of queries (in the 'q' parameter) but nothing gets close to this.
As you can see, I want as many columns about the pages as possible.
The closest I've come is this far: https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=search%3Fq%3Dcoffee%26type%3Dpage%26center%3D37.76%2C-122.427%26distance%3D1000&
Does anyone know how I should think/do in order to get this data in the new way?
Have a look at
https://developers.facebook.com/docs/graph-api/reference/page#Reading
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.3#fields
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.3#search
You can specify the desired result fields according to the fields which are available for the respective object.
For example
/search?q=coffee&type=place&center=37.76,-122.427&distance=1000&fields=id,name,hours,category,category_list,about,bio,description,general_info,location,talking_about_count,were_here_count,likes,phone,cover,website
Try it
https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=search%3Fq%3Dcoffee%26type%3Dplace%26center%3D37.76%2C-122.427%26distance%3D1000%26fields%3Did%2Cname%2Chours%2Ccategory%2Ccategory_list%2Cabout%2Cbio%2Cdescription%2Cgeneral_info%2Clocation%2Ctalking_about_count%2Cwere_here_count%2Clikes%2Cphone%2Ccover%2Cwebsite
Keep in mind that the fieldnames are not the same from FQl to Graph API.

Facebook - get user likes and comments

Is it possible to get the user likes and comments from facebook?
With the graph api, you can can do me/likes, but that only returns the pages you liked, and I want all the likes. So also the url's, posts, pictures, ... I liked. The same for the comments.
Or is there a query to get the activity log from an user?
You can use the
FQL like table (https://developers.facebook.com/docs/reference/fql/like/)
FQL comment table (https://developers.facebook.com/docs/reference/fql/comment/)
to query for object comments and likes. Then, if you want to distinguish between object types (photo, album, event, group, note, link, video, application, status, check-in, review, comment, post) for getting more info about the like or comment, you need to do queries like this for each object type:
select src, link from photo where object_id in (select object_id from like where user_id=me() and object_type="photo" limit 25)
For URL likes you can use
FQL url_like table https://developers.facebook.com/docs/reference/fql/url_like/
You can use the Graph API. You have to fetch them with different end-points, like:
Get the posts with: /me/feed - demo
Perms req: read_stream
Get the photos with: /me/photo - demo
Perms req: user_photos
Now, to get the likes use this:
/likes?id={post_id|photo_id|url}
for comments:
/comments?id={post_id|photo_id|url}
You can test your calls in Graph API Explorer

How to use graph api to post comments

If I have 10 friends how do i get the latest status for each of them and then post a comment on all of these statuses?
how do you get the post ID?
$facebook->api('/'.$POST_ID.'/comments','post',array('message' => $comment));
I can't find a way to get friends statuses ordered by date from the Graph API, so you'll have to use FQL. This query will get you the 10 most recent status updates by your friends:
SELECT uid,status_id,time,message FROM status WHERE uid IN
(SELECT uid2 FROM friend WHERE uid1 = me()) ORDER BY time DESC limit 10
There is no guarantee that these 10 will come from different friends since there is no way to execute a UNIQUE query in FQL. If you want to ensure you're commenting on 10 unique friends' statuses, you'll have to request more results and keep track of whose posts you're commenting on in your script.
From here, you can loop through these results and set $POST_ID = $result['status_id'] then execute your API call above.
If this is for a public app you're building, you'll want to look at combining these 10 updates together into a single batch.request to keep from running into the API call limits.

Facebook API FQL multiquery to get actor name as well as ID

I am making an FQL query using the Facebook PHP API, like so:
$stream = $this->facebook->api('fql', array(
"q" => "SELECT post_id, actor_id, message, description, description_tags, type, attachment
FROM stream
WHERE filter_key in (
SELECT filter_key
FROM stream_filter
WHERE uid=me() AND type='newsfeed'
) AND is_hidden = 0"
));
This gives me the user's news feed. However, this will only provide me with the ID of the actor - I need the actor's name. (By actor, I mean the author of the story in the news feed.)
I understand that I will need to query the API again to retrieve the name. How do I perform such a multiquery? Using this example, how can I get the actor name?
Most people's initial reaction would be to suggest a multi-query. However I dont suggest it for the following reason: It is possible you app can already knows the name of the actor.
Let's say you do lots of these calls to get stream items (one of my production applications does and I've got it coded the way I describe). What you don't want to do is overload the API and make Facebook unhappy with your app id and start reducing your call limits!!
So, what you can do is cache (using memcached, web application cache, database, or other) the user name (as the name to id rarely changes for a give id) based upon the actor_id. This is also valuable to find the to/from ids for other facebook fql objects like comments, likes, etc.

Facebook API: How do you get the url for the last created picture?

How can I get the url of the picture I just uploaded to facebook through the API?
I need the user to make it its profile picture and I want to give him a fast link to go there.
Alright, so using the Graph API you need to do this:
Retrieve a list of of all albums
Determine which album was changed most recently
Retrieve a list of photos in that album
Determine which photo was changed most recently
Here's how you do it:
First you call: https://graph.facebook.com/me/albums
Which gives you a list a list of all the albums, each album has a key called "updated_time". Loop through all the albums and compare them to see which one is the most recent one.
When you have found the most recent one you take the id of that album and call: https://graph.facebook.com/{ the album id }/photos
This will give you a list of all the photos in that album. Each photo has a key called "created_time". Use that to determine which photo is the most recently created. When you have done that you can just take the value in the key "source" which is an url to the full scale of the picture.
If you want a smaller picture there's a key called "images" which contains a list of 4 different sizes; 590x480, 221x180, 130x105 and 92x75. Each of those items in the list has the "source" key.
You can use FQL to do this very fast by issuing the following query:
SELECT object_id FROM photo WHERE aid IN(SELECT aid FROM album WHERE
owner = me() ORDER BY modified DESC LIMIT 1) ORDER BY modified DESC
LIMIT 1
This will return just the graph object id, which you can then simply issue a graph API request to https://graph.facebook.com/object_id
If you want more fields than just the object_id, check these docs: http://developers.facebook.com/docs/reference/fql/photo/

Categories