I have a problem with API call on Facebook Graph API. This API call (with the php-sdk):
$posts = $facebook->api('/me/posts?since=2010-1-1&until=2010-3-31T23:59:59');
returns empty array, that looks like this using var_dump : { ["data"]=> array(0) { } }
If I modify the call like this:
$posts = $facebook->api('/me/posts?since=2010-1-1');
it returns the right posts. If I modify it like this:
$posts = $facebook->api('/me/posts?until=2010-3-31');
It returns empty array again. However, this
$posts = $facebook->api('/me/posts?until=2010-12-31');
works fine (but for different posts, than I want).
All of the above, with "statuses" or "links" instead of "posts", return non-empty array.
What is going on?
I think you are hitting infamous 5000 limit. Facebook acts like if you always have only latest 5000 records. So if your 5000th entry from top was newer than 2010-3-31 it won't return anything for /me/posts?until=2010-3-31 query.
Run /me/posts query and look at the bottom item. Facebook won't return anything older than it no matter what extra query parameters you supply (undocumented feature).
Related
I would like to show the latest N Facebook posts on my website.
I am using this simple code:
var FB = require('fb')
FB.api('/flourandfire/posts', 'get', { fields: [ 'message', 'picture' ], access_token: '1694494XXXXXXXX|1ba36298123bbf9689942fXXXXXXXXXX' },
function (res) {
if (!res || res.error) {
console.log(!res ? 'error occurred' : res.error)
return
}
console.log(res)
})
However, I need to be able to have some kind of filtering, since there is "noise" in the feed due to the direct link with Instagram (which results in short posts with a picture, that I do NOT want to include in the website's feed)
I basically need to somehow "differentiate" specific posts, which will then placed on the site.
I could fetch 100 posts and filter myself based on a specific tag (like #pub; however, there is the risk of having lots of Instagram posts, more than 100, and end up with ZERO posts on the website.
How would you solve this issue?
There is no official filtering, i would do it like this:
Get N entries by setting the limit parameter to N
Filter them on your own
If there are less than N elements after filtering, use another API call to get more items
Repeat from Number 2
Just an idea though, you could also increase the initial limit - but it would usually result in a slower API call so be careful with that.
Is it possible in using the Soundcloud Javascript API to order results by like count? Everything I can find says you can no longer order by 'hotness' but nothing about other sorts. I've tried adding an order parameter to my API call such as:
SC.get('/tracks', {
limit: 200,
genres: 'electronic',
order: 'likes_count'
}).then(function(tracks) {
console.log(tracks);
});
But the API call returns a 400 Bad Request, as soon as I remove the order: 'likes_count' line the API call works again.
The order parameter is no longer in the SoundCloud API.
I am using facebook4j to collect posts on a particular topic.
The code runs all find and I am able to search the topics.
But the result also contains the irrelevant data. Example if i search a keyword such as "messi", i am expecting the posts that contains the word "messi". But the result contains the posts of a user whose name is "messi". How can i avoid this problem?
My code is as follows
Facebook facebook = FacebookFactory.getSingleton();
facebook.setOAuthAppId("********", "*****************");
facebook.getOAuthAppAccessToken();
ResponseList<Post> results = facebook.searchPosts("messi");
for (Post result : results) {
System.out.println("************");
System.out.println(result);
}
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 requesting this page to get the events with the keyword
"conference":https://graph.facebook.com/search?q=conference&type=event
This works fine.
The problem is the pagination returned:
"paging": {
"previous":"https://graph.facebook.com/search?q=conference&type=event&limit=25&since=2010-12-18T17%3A00%3A00%2B0000",
"next":"https://graph.facebook.com/search?q=conference&type=event&limit=25&until=2010-11-04T16%3A29%3A59%2B0000"
}
It seems to have more events with "conference", but requesting these 2 pagination URLS returns no data.
It's weird because it's the same for any requested keyword, and the pagination URLs returned by the Facebook API seems to always returns empty data.
Does anyone know what's the issue?
Thanks
I encountered similar confusion with a query against places. The "next" URL behaved exactly as you described it.
I could query location information using a url like this:
https://graph.facebook.com/search?access_token=INSERT_TOKEN&type=place¢er=55.8660,-4.2715&distance=150&limit=10
And got back JSON with the first 10 places plus the following fragment which suggests the existence of paging params:
"paging": {
"next": "https://graph.facebook.com/search?access_token=INSERT_TOKEN&type=place¢er=55.8660\u00252C-4.2715&distance=150&limit=10&offset=10"
Hitting that URL doesn't work. But I did figure out a combination of limit and offset params that gave me effective paging.
limit=10 & offset not defined => first 10 results
limit=20 & offset=10 => next 10 results
limit=30 & offset=20 => next 10 results
limit=40 & offset=30 => last 8 results (can stop here because less than 10 back)
limit=50 & offset=40 => confirmation that there are no more results
I realise that I've got "limit" and "offset" rather than the "limit" and "until" params that you get, but, hopefully you could apply the same technique i.e. keep incrementing the limit and inc the date/time to that of your last result?
I think this is a standard practice in Facebook Graph API. I think if your request resulted to a non empty JSON, they will always give you the next paging, even though it might be empty.
I am however not 100% sure, because Facebook Graph API does not seem to be very well documented... (for example they said we can modify this pagination thing but did not explain clearly how to do it).
Seems facebook has changed it recently.
Here's the fix:
For a datetime returned in next and previous as
"2011-01-18T08\u00253A42\u00253A35\u00252B0000",
replace all occurrences of "\u0025" with "%" and it should work fine.
If you notice the facebook's datetime format, it is
2011-01-18T08:42:35+0000
(date accepted by strtotime C function)