Are data acquired from Facebook GRAPH API real-time? - facebook

I am creating a Facebook application that will download all the photos in an album. The application is created for my personal use and at the same time, learn the Facebook API and JSON.
I can already retrieve the URL of the photos inside an album by calling this url:
http://graph.facebook.com/[album id]/photos?fields=source
The album that I'm trying to download contains 5400+ photos so I tried increasing the limit by adding the limit parameter:
http://graph.facebook.com/[album id]/photos?fields=source&limit=1000
Here's the problem:
The results being returned are only until 2010-07-30T11:20:11+0000. When I tried to modify the query by using the until parameter like so:
http://graph.facebook.com/[album id]/photos?fields=source,link&limit=1000&until=2010-06-01
the data responded correctly. However, if I changed the date to something like 2010-08-05, the latest photo returned will have a created_date of 2010-07-30T11:20:11+0000.
The last photo returned is photo #5000 out of 5695.
Here's my question:
Is the data acquired from Facebook GRAPH Api real-time (or a Monthly update, 2010-07-30)? Or there's just a limit on the number of photos returned on album (like 5000)?
Thanks!
EDIT
There is a 5000 object limit in Facebook. If you know how to break the limit, go here:
Breaking the 5000 object limit in Facebook API
Thanks!

There is indeed 5000 limit on returned objects. You would need to run multiple FQL queries on photo table ordering and limiting the results (<5000) to get all the data (check photo table page for examples). (doesn't work)

When I look at the photos in the graph query you linked
https://graph.facebook.com/119403264763178/photos
I see paging information at the bottom. So, hacking together a quick test
$request = 'http://graph.facebook.com/119403264763178/photos?fields=source&limit=1000';
$response = json_decode( file_get_contents( $request ), true );
$totalCount = 0;
while ( count( $response['data'] ) )
{
echo 'Fetching ' . urldecode( $response['paging']['next'] ) . '<br>';
$totalCount += count( $response['data'] );
$response = json_decode( file_get_contents( $response['paging']['next'] ), true );
}
echo $totalCount;
It would seem that even following the paging data, you can still only retrieve 5000 records.
I'd suggest hitting up the FB forums or opening a bug ticket.

Related

Get the new like growth with Facebook API

I am using the Facebook API to pull data in JSON format.
I need to pull the new likes growth from the pages that im not an admin.
$pageContent1 = file_get_contents('https://graph.facebook.com/v2.5/' . $page . '/?fields=emails,about,website,category,likes.summary(true),location,new_like_count,name,were_here_count,cover,phone,posts&access_token=' . $token . '');
$parsedJson1 = json_decode($pageContent1);
You can only get this if you do one call on day one, and then another calls for every subsequent days if you're not an admin of the page.
See
https://developers.facebook.com/docs/graph-api/reference/insights/#reading
The only publically available insight metrics are page_fans_country and page_storytellers_by_country, but they are both lifetime metrics, meaning that you only get the absolute value.

Facebook Request Dialog with data

I read this article.
So, I tried it and I put a number in the data property.
FB.ui({
method: 'apprequests',
message: 'Come join me and play at MyWebSite!',
data: '12345',
redirect_uri: 'myWebSite'
});
I get the request_ids, but how do I get the data part (the 12345 number)?.
on server side, you can do something like:(using php here)
$request_ids = $_GET['request_ids'];
$request_ids = explode(",", $request_ids);
foreach($request_ids as $request_id)
{
$request_object = $facebook->api($request_id);
if(isset($request_object['data'])) $req_data = $request_object['data']; //$req_data will be '12345' as per your request data set.
// after getting the data, you may like to delete the request.
$full_request_id = $request_id."_".$fbid; //$fbid is current user facebook id
$facebook->api("$full_request_id","DELETE");
}
Did you try Facebook's documentation too?
https://developers.facebook.com/docs/requests/ has more documentation; if a data parameter was added in the call to the requests dialog, the same value should also be there when requesting the Request details via the API (i.e. a call to /REQUEST_ID)
See the facebook developer site documentation for more details
http://developers.facebook.com/docs/reference/dialogs/requests/
Note:
data:Optional, additional data you may pass for tracking. This will be stored as part of the request objects created. The maximum length is 255 characters.

facebook graph api check if user is a member of a group using PHP

i want to check if a user is a member of a group using facebook graph api...
i have this:
$group = file_get_contents("https://graph.facebook.com/177129325652421/members?access_token=XXXXXXXX");
$group = json_decode($group);
$checkuser = $group->data;
and check the user if is a member by using his facebook id and in_array()
if(in_array($_GET["fid"],$checkuser)){
echo "yes";
} else {
echo "no";
}
can someone help me to correct this please... my code is not working...
Reference: https://developers.facebook.com/docs/reference/api/
Use the API url:
https://graph.facebook.com/me/groups
To get a user's groups. In the above link, change the me/ to the user's FB ID. You must also pass in an Access Token.
The reply will be JSON encoded. Decode it using json_decode to a PHP Associative array. Iterate over it and check for the group you want.
The Graph API does not return all groups at once. You must either use the pagination links at the end of each response to fetch more, or use the limit parameter to request as many as you need.
The following code sample will post the IDs of the Groups you are a part of
<?php
$url = "https://graph.facebook.com/me/groups?access_token=AAAAAAITEghMBAMDc6iLFRSlVZCoWR0W3xVpEl1v7ZAxJRI3nh6X2GH0ZBDlrNMxupHXWfW5Tdy0jsrITfwnyfMhv2pNgXsVKkhHRoZC6dAZDZD";
$response = file_get_contents($url);
$obj = json_decode($response);
foreach($obj->data as $value) {
echo $value->id;
echo '<br>';
}
/* to check for existence of a particular group
foreach($obj->data as $value) {
if ($value->id == $yourID) {
//found
break;
}
//not found. fetch next page of groups
}
*/
PS - If running the above code gives you an error stating Could not find wrapper for "https", you need to uncomment/add the PHP extension extension=php_openssl.dll
Was looking into this and found this as first answer in google but the answer seems to be much of a hassle so I dug a bit deeper.
The fastest answer I've found which doesn't require iterating through all of the groups' members uses FQL.
SELECT gid, uid FROM group_member WHERE uid = (user id) AND gid = (group id)
This either returns an empty 'data' object, or a 'data' object with the UID and GID.
It also (from what I see so far) , doesn't require the user_groups permission.
https://developers.facebook.com/tools/explorer?fql=SELECT%20gid%2C%20uid%20FROM%20group_member%20WHERE%20uid%20%3D%20551549780%20AND%20gid%20%3D%20282374058542158
This FQL query returns for me:
{
"data": [
{
"gid": "282374058542158",
"uid": "551549780"
}
]
}
This doesn't seem to be possible after Graph API v2.4, because Facebook decided to disallow it:
https://developers.facebook.com/docs/apps/changelog#v2_4
"the user_groups permission has been deprecated. Developers may continue to use the user_managed_groups permission to access the groups a person is the administrator of. This information is still accessed via the /v2.4/{user_id}/groups edge which is still available in v2.4."
It also states "From October 6, 2015 onwards, in all previous API versions, these endpoints will return empty arrays." But it seems to me that it still works on v2.2 & v2.3.

I am having problems running Facebook FQL queries that include long user ids

I am having problems running queries with FQL that include a supplied "Large"(beginning with 10000..) User ID
here is an example of one that is not working:
fql?q=SELECT uid, first_name,last_name,pic,pic_square,name
FROM user
WHERE uid=100002445083370
Is there a way to encapsulate the long number so it's passed as a string?
here is another example:
/fql?q=SELECT src_big
FROM photo
WHERE aid IN (SELECT aid
FROM album
WHERE owner=100002445083370 AND type="profile")
ORDER BY created DESC LIMIT 1
Has anyone been able to solve this issue? I am testing the queries in the graph explorer with no luck as well.
I see what the problem is,
The User id I am trying to pass is supposed to be: "100002445083367", but from querying the list of friends and grabbing their User Id, I am getting back "uid":1.0000244508337e+14 which is being shortened to: 100002445083370 (php removing the e+14) throwing off the second query. I need to make sure the id I am grabbing is staying as a string value not a number while I pass it back and forth from PHP and Javascript.
The problem is because of the way PHP handles JSON_DECODE. I had to modify Facebook PHP SDK and add a preg_replace previous to the json_decode. It will make sure json_decode doesn't convert large integers to floats by first converting them to strings.
here is the code:
line 803 from base_facebook.php:
$result = json_decode(preg_replace('/("\w+"):(\d+)/', '\\1:"\\2"', $this->_oauthRequest($this->getUrl($domainKey, $path),$params)), true);
here is more information on the subject:
http://forum.developers.facebook.net/viewtopic.php?id=20846
What do you mean by "not working"?
That query works for me in Graph API explorer but the response is
{
"data": [
]
}
I think that user-id isn't valid; https://www.facebook.com/profile.php?id=100002445083370 gives a "Page not found" error for me.

Facebook Graph API - get a photos album ID

I'm writing an application that uploads a photo to facebook. I'm not supplying an album ID so an album is created on behalf of my app.
My question is: how do I get the album ID the photo was uploaded to?
When I GET the the photo information the album isn't present in the JSON which was my hope.
Jimmy Sawczuk's answer is close, but doesn't work because FQL Photo table columns aid and pid are Rest/FQL API ids, not Graph API ids.
Fortunately the object_id and album_object_id columns in this table are indexable (contrary to the documentation) so this should give you both Rest/FQL & Graph ids for the album:
select aid, album_object_id from photo where object_id = ${GRAPH_PHOTO_ID}
In general object_id fields in Rest/FQL API responses are Graph API id (except for photo notifications, where it's the Rest/FQL id).
Re HonkyHonk's answer: not all photo link urls have a set parameter; in this case I think you can find the Rest/FQL API photo id from its other parameters, and then use Rest/FQL API to get the album id:
For users with 32-bit user-ids: calculate (id << 32) | pid
eg for http://www.facebook.com/photo.php?pid=7518353&id=549683637 this gives 2360873244068853905
Otherwise concatenate id, _ and pid
eg for http://www.facebook.com/photo.php?pid=155052&id=100002937903251 this gives 100002937903251_155052
It would help a lot if Facebook could document something about the two types of ids. Also the Graph API Photo schema should include the album's Graph API id.
This is the easy way to get it :
This issue has been bugging me for a while as well, but I think I have cracked the nut.
The album ID is hidden in the link value, so if you have a photo-post object from the Graph API, you can retrieve the Album ID like this:
NSString *link = [item valueForKey:#"link"];
NSString *album_id = nil;
NSRange fRange = [link rangeOfString:#"set=a."];
if(fRange.location != NSNotFound) {
NSInteger firstPos = fRange.location + 6;
NSInteger endPos = [link rangeOfString:#"." options:NSLiteralSearch range:NSMakeRange(firstPos, 25)].location;
album_id = [[link substringWithRange:NSMakeRange(firstPos, endPos - firstPos)] copy];
}
It could probably all be stripped down to one line of code, but I think this is more readable.
For getting the album information use:
[facebook requestWithGraphPath:album_id andDelegate:delegate];
For getting all the photos in an album use:
[facebook requestWithGraphPath:[NSString stringWithFormat:#"%#/photos", album_id] andDelegate:delegate];
Hope this can help someone.
If I'm understanding you correctly, you have the photo ID but need the album ID. You could try using the photo FQL table with the following query:
SELECT aid, pid FROM photo WHERE pid = '<your photo id>'
Long, clunky, but theoretically valid way:
http://graph.facebook.com/userid/albumswill get you a list of all the albums the user lets you see, then you can search the album objects (api here) for created_times that match the time you created the album, which you can store when you create the album.
This is almost certainly a horrible way to do it, but it should work.
Richard Barnett's solution works well for me, I use this as an FQL query through the Graph API to get a JSON response like I would for any other Graph API query:
https://graph.facebook.com/fql?q=select+album_object_id+FROM+photo+WHERE+object_id='[PHOTOID]'
This returns:
{
"data": [
{
"album_object_id": "10150531092908837"
}
]
}
Then you can access the album ID using:
<?php
$strPhotoAlbumID = $objFacebookAPI->data[0]->album_object_id;
?>
This is the cleanest solution i've found as Facebook constantly change the format of their photo and album URLs so parsing these with RegEx wont work as a long term fix.
I ended up just using the legacy REST api since the album ID is passed with the link to the image in the server response. I then used a regex to pull the id out.
Not the best solution and I wouldn't recommend it but I feel its better than adding another several server requests to the code.
function get_album_id_by_album_name($album_name){//return array of data
$fql = 'SELECT aid, owner, name, object_id FROM album WHERE owner=me() and name="'.$album_name.'"';
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $this->facebook->api($param);
return $fqlResult;
}
String albumID = "https://www.facebook.com/photo.php?fbid=192570127478130&set=a.135556629846147.24342.100001754323389&type=1";
albumID = albumID.split("&set=a.")[1];
albumID = albumID.split("\\.")[0];
System.out.println(albumID);