Facebook graph api comments pagination - php

I'm trying to get all the comments of post by using the following api url request:
$fb->get($yourPage.'/feed?fields=comments.limit(100)&limit=25&since=2017-7-01&until=2017-7-31', $accessToken);
But some posts have more than 500 comments which yields pagination '(next)' in the api response. Now I want to retrieve all the comments, so how do I paginate through the "comments" because I know how to paginate through posts through this code:
$response = $fb->get($yourPage.'/feed?fields=comments.limit(500)&limit=25&since=2017-7-01&until=2017-7-31', $accessToken);
$comments = $response->getGraphEdge();
$totalcomments = array();
if ($fb->next($comments)) {
//Do something if there is pagination in the posts
}
So how do I do the same for comments? I've searched alot on stack and google but no questions point to this... Thanks

Each comments object includes a paging object for that:
{
"comments": {
"data": [
{
"created_time": "2017-09-01T12:06:19+0000",
"from": {
"name": "xxx",
"id": "1234"
},
"message": "xxx",
"id": "1234"
},
...
],
"paging": {
"cursors": {
"before": "NzkZD",
"after": "NzgZD"
},
"next": "https://graph.facebook.com/v2.10/cccccc/comments?access_token=xxx&pretty=0&limit=2&after=NzgZD"
}
},
"id": "xxx"
}
For every post you get, you need a subroutine (for example, with a recursive function) to use the "next" parameter in a separate API call to get all comments.

Related

shareable link of messanger using facebook graph api

I am using a Facebook graph to fetch comments on particular posts. The response is something like this
{
"data": [
{
"message": "43211234",
"created_time": "2021-01-09T05:33:14+0000",
"from": {
"name": "USER NAME",
"id": "USER ID"
},
"id": "COMMENT ID"
}
],
"paging": {
"cursors": {
"before": "M...",
"after": "M.."
},
"next": "https://graph.facebook.com/v9.0/10...."
}
}
the response looks good, but I also want a shareable messenger link of the user who has commented so that I can talk to that person later. is there any solution for this? I searched for it but could not find any solution.

Accessing array inside array

I'm working on my task right now, which accessing specific array when it is called by front end.
My example data is like this
{
"data": [
{
"name": "Jon Snow",
"id": "01-0001",
},
{
"name": "Robert Stark",
"id": "01-0002"
},
{
"name": "Sansa Stark",
"id": "01-002333"
},
{
"name": "Arya Stark",
"id": "01-00012"
},
{
"name": "Bran Stark",
"id": "01-0003"
},
{
"name": "Rickon Stark",
"id": "01-0005"
}
]
}
* In my front end I have this code *
selectedEmployee: any=null;
setActiveEmployee(employee: any) {
this.selectedEmployee = employee;
let myJSON = JSON.stringify(this.selectedEmployee);
this.perEmpLateEarly();
}
Whenever I choose the employee i get the id of that employee.
So in this, if i choose "id": "01-0001" it will return the first array and it will keep the rest, and if I choose "id": "01-0002" it will return the second one and will keep the rest and so on. How can I do that in Php?
Thanks in advance
You will do a GET/POST HTTP request from frontend, which would look something like this in
your_backend_page.php?id=<ID HERE>
Then the "your_backend_page.php" would look like as follows:
$list = { "data": [ { .... }, { ... }] } // this is the array you have
$idFromFrontEnd = $_GET["id"];
foreach ($list["data"] as $item) { // iterate over all items
if ($item["id"] == $idFromFrontEnd) { // check if id matches
echo json_decode($item); // if matches, print this item
}
}
Please note this approach is okay if you have a small number of items. For a larger list, you might want to have a database, where you can use sql to select.

Facebook - Get & Display me/og.likes in Video application

Have built an video app that publish user actions towards Facebook.
In this app i have implemented an "Favorite" function that i have hooked up towards a basic open graph action "og.like"
I want to be able to display video's that user liked and apply my own styling to that.
Basically i want to display "Title" "Url" & "Image"
So i use the PHP-SDK towards authored user with active access token and execute
$response = $facebook->api(
'me/og.likes',
'GET'
);
// handle the response
How do i now sort out my correct fields and display them ?
Am not hardcore at either php or javascript but will be able to sort this out if i just can get a little push in the right direction. Like just showing the raw data
Update
Finally a little progress, adding
print_r ($response);
Will write out the raw data, Now i know that am on the right way.
Array returned
{
"data": [
{
"id": "123",
"from": {
"name": "Mathias",
"id": "APP_ID"
},
"start_time": "X",
"end_time": "X",
"publish_time": "X",
"application": {
"name": "APP_Name",
"namespace": "",
"id": "321"
},
"data": {
"object": {
"id": "139",
"url": "Url to like",
"type": "video.tv_show",
"title": "title"
}
},
"type": "og.likes",
"no_feed_story": false,
"likes": {
"count": 0,
"can_like": true,
"user_likes": false
},
"comments": {
"count": 0,
"can_comment": true,
"comment_order": "chronological"
}
},
And then the next..
From every app "like" i would like to display Url ,Title & Image
From what i understand so far my main problem is that this is nested arrays, Did try with single level arrays and there i did manage to display correct data just by
echo $response[name];
So how do i digg in and loop this around, All tips are welcome,
{
"id": "139",
"url": "url",
"type": "video.tv_show",
"title": "titke",
"image": [
{
"url": "image_URL",
"secure_url": "image_URL",
"type": "image/jpg",
"width": 1024,
"height": 576
}
Here's an example:
<?php foreach ( $response['data'] as $data ): ?>
<?php $Object = $data['data']['object']; ?>
<?php echo $Object['title']; ?><br />
<?php endforeach; ?>

How can i get the admins user id of a page in facebook.?

How can i get the admins user id of a page in facebook using grapg API .?
For eg: This will give the details of page KFC
https://graph.facebook.com/126380033352
{
"id": "126380033352",
"name": "I Love KFC",
"picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/50510_126380033352_4943708_s.jpg",
"link": "http://www.facebook.com/Official.KFC",
"likes": 543485,
"category": "Food/beverages",
"username": "Official.KFC",
"founded": "1929",
"about": "KFC United Kingdom. Finger Lickin' Good.",
"can_post": true,
"talking_about_count": 8965,
"type": "page"
}
I need to get the user id of the page admin..? I have checked the permissions but i dont know how to get that .?
First: User must be a admin of that page.
With "manage_pages" permission, request GET https://graph.facebook.com/[PAGE_ID]?fields=access_token
In response will get a Page admin access_token.
Now use the page admin access_token to request GET https://graph.facebook.com/[PAGE_ID]/admins
In response you will get the list of admins, their id and usernames.
Reference:
http://developers.facebook.com/docs/reference/api/page/
You cannot get all admin users of the app but you can test any uid.
Firstly you need to grant 'manage_pages' permission then query page_admin table.
FQL implementation for current user looks as following:
$isAdmin = false;
$hasManagePerm = false;
try{
$responses = $facebook->api('fql', 'GET', Array('q' => Array(
'hasManagePerm' => 'SELECT manage_pages FROM permissions WHERE uid=me()',
'isAdmin' => 'SELECT 1 FROM page_admin WHERE uid=me() AND page_id=' . APP_FB_ID,
)));
foreach ((Array)$responses['data'] as $response) {
if($response['name'] == 'hasManagePerm') {
$hasManagePerm = !empty($response['fql_result_set'][0]['manage_pages']);
} else if($response['name'] == 'isAdmin') {
$isAdmin = !empty($response['fql_result_set']);
}
}
} catch (Exception $ex) {
}
if(empty($hasManagePerm)) {
throw new AdminPanelAccessRestrictedException("perm");
}
if(empty($isAdmin)) {
throw new AdminPanelAccessRestrictedException("admin");
}
WHERE APP_FB_ID - an application id
If you don't like the Graph objects, you can always use FQL
fql?q=SELECT uid, page_id, type FROM page_admin WHERE page_id = {pageId}
returns this:
{
"data": [
{
"uid": "6905135096",
"page_id": "{pageId}",
"type": "ATHLETE"
},
{
"uid": "14408356540",
"page_id": "{pageId}",
"type": "ATHLETE"
},
{
"uid": "51936644133",
"page_id": "{pageId}",
"type": "ATHLETE"
},
{
"uid": "7230226073",
"page_id": "{pageId}",
"type": "ATHLETE"
}
]
}
#Sarim's link of '/admins' is deprecated after Graph API 2.1.
FB Graph API 2.2 and above use '/roles'

How do I access deeper levels of a json tree using curl?

I'm trying to learn how to use CURL and Json to access data sent from the Facebook graph api.
I'm using the following function which pulls the post data:
function loadFB($fbID){
$url="https://graph.facebook.com/".$fbID."/feed?limit=1&access_token=xxxx";
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$page = json_decode(curl_exec($c));
curl_close($c);
$post=reset($page->data);
if($post->message == '') {
$post_msg = '' . $post->name . '';
} else {
$post_msg = $post->message;
}
return $post_msg;
}
Example JSOn data looks like this:
{
"data": [
{
"id": "xxxx",
"from": {
"name": "xxx",
"category": "xxx",
"id": "xxx"
},
"picture": "xxxxxxxxx",
"link": "xxxxxxx",
"name": "Event name goes here",
"properties": [
{
"text": "Friday, July 15, 2011 at 4:00pm"
},
{
"text": "Venue Name"
}
],
"icon": "http://static.ak.fbcdn.net/rsrc.php/v1/yW/r/r28KD-9uEMh.gif",
"type": "link",
"object_id": "xxx",
"created_time": "2011-06-23T06:46:17+0000",
"updated_time": "2011-06-23T06:46:17+0000",
"likes": {
"data": [
{
"name": "xxxx",
"category": "xxx",
"id": "xxxx"
}
],
"count": 1
}
}
],
"paging": {
"previous": "xxxxx",
"next": "xxx"
}
}
As it stands, I can retrieve the message of a page update, or if its an event I can retrieve the event name and the link for the event.
But what if I want to retrieve say the event date or the venue name? Its under another tier of 'properties'.
With my code so far, I can access the first level of things with $post->message, but when I try $post->properties->text this doesn't work - so I don't understand how this works. On top of that, in the 'properties', there's 2 'text' which is adding to my confusion of how to access these things.
Any pointers?
$post->properties is an array so :
$post->properties[0]->text;
Properties in the data above is an INDEXED ARRAY so you want $post->properties[x].text where x is 0 for the first item and 1 for the second item, etc...

Categories