How to use graph api to post comments - php

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.

Related

Get a photo with most likes in facebook album

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

Rerequesting posts from mysql database social network site

I am working on a social network website project. I have created database and everything.
The posts table has a preference column which stores the preference value according to the likes and comments that a post gets from the users and also the time at which the post is created.
To retrieve posts for a user's home page from the posts table, I am running a query using joins which sorts using preference column .
Now, suppose I retrieve 10 posts for a user to be shown on the posts table and user scrolls down and one more request is made from the user to retrieve next 10 posts to the server.
If in between of those requests few other users creates a new post or preference value of posts in the database changes in the between, and now if I the second request is run on the server, all the posts will be resorted for the second request (i.e. to show next 10 posts) but since the database is updated , this means in the second request there will be many chances that few of earlier 10 posts are retrieved along in the second request.
I want to know how to avoid these duplicate requests.
How facebook or any other social network solves this problem at the backend when their database is dynamic.
I would rather avoid such unreliable way of sorting at all.
As a user, I'd rather quit that service. Frankly, I hate such too smart a service which decides which posts I have to see and which not. And even dynamically ordered on top of that.
Make it ordered by date, by tags of interest, by something sensible, reliable and constant.
In your script store a record of the rows id returned.
For example, using a basic limit and just storing the latest id when the first select is done, and using the page number to determine the limit of records to return.
SELECT id, somefield
FROM SomeTable
WHERE id < $SOMESTOREDVALUE
LIMIT $PAGENUMBERTIMESTEN, 10
or storing the latest id after each page is returned (which you will need to store each time this is run)
SELECT id, somefield
FROM SomeTable
WHERE id < $SOMESTOREDVALUE
LIMIT 0, 10
If you store the time & date when the user first makes a request in a session, you could use that to filter the posts table.
So your SQL for the second page of results would be along the lines of
SELECT <some fields> FROM <sometables>
WHERE DatePosted <= $timefirstseen LIMIT 10, 10
Where $timefirstseen was loaded from the session variable. This will restrict your results to only posts that existed when the users visit started.
You would of course need to include a feature to allow the user to clear the session or do that automatically when they revisit their homepage to make sure they got to see the new posts eventually!

getting twitter followers of specific user

my site needs to keep track of followers of certain users
if i want to get the names of all the followers of specific user lets say this user have 1000 follower
i will be able to get the follower ids by https://api.twitter.com/1/followers/ids.json
1000 id
then to get the username of each id i will have to run https://api.twitter.com/1/users/lookup.json
for each id of those 1000 id but the rate limit of 350 makes this impossible
any solution , what i am doing wronge ?
ok i found the solutino actually in users/lookup as parameter of user_id i can send multiple ids saperated by commas
so in single API call i can send upto 100 ids which will mean in one hour i can get the lookup of 35000 users which is more than enough for most of the cases

Get the last three statuses and posts on Facebook

I am trying get the last three posts and statuses, as I know the status table contains only the status that I put on my Facebook page. The posts table contains a shared link, posts made by me or other users, so I want to: compare the last three statuses with the last three posts and take only the most 3 new (created date) from them, so I need to make check the created date or updated date for both sides and the output should be three.
I have tried:
Take the last three posts and the last three statuses, compare the updated date with the most updated three, put them in an array. That took a lot of PHP code. Can I do that with FQL?
You can get the last three posts and statuses via Graph API. Use:
https://graph.facebook.com/fb_id?fields=id,name,statuses.limit(3).fields(message),posts.fields(message,caption,description)
For more help, https://developers.facebook.com/tools/explorer
FQL
For getting statuses
SELECT status_id, message FROM status WHERE uid=fb_id
For getting posts
SELECT post_id, actor_id, target_id, message FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid=me() AND type='newsfeed') AND is_hidden = 0
But for all these you should have the permissions for accessing them. See
http://developers.facebook.com/docs/reference/fql/stream/.

Facebook open graph, filtering friends list array

I want my application to filter the friends list I get using the open graph. People who login into my site must see only their friends who also use my app. I've noticed this on many applications like Quora, Thumb etc. How can it be done using php?
Can easily be done when you use FQL to query the user table (in conjunction with the friend table) – that has a field is_app_user, so a query like
SELECT uid, name FROM user
WHERE uid IN (SELECT uid1 FROM friend WHERE uid2 = me())
AND is_app_user
will return only those friends of the current user that are also users of your app.
You can access the lists of a user by access the friendslist connection via graph api
example: http://developers.facebook.com/tools/explorer/?method=GET&path=me%2Ffriendlists
/me/friendlists?access_token='.$access_token.'

Categories