Facebook API get post details from feed [duplicate] - php

This question already has answers here:
Facebook JS SDK's FB.api('/me') method doesn't return the fields I expect in Graph API v2.4+
(4 answers)
Closed 7 years ago.
I'm using the PHP Facebook API (together with laravel)
I can manage to get the feed from a page, with all it's post using
$request = Facebook::get('/COMPANYPAGE?fields=feed.limit(25)');
$response = $request->getGraphPage()->getField('feed');
This returns the following objects
But I can't seem to get the specific information for that post! Like
the images included.. Is there another call I need to perform?
I tried the following but it's giving me back the same results
$request = Facebook::get('/elbeko?fields=feed.limit(25)');
$response = $request->getGraphPage()->getField('feed');
foreach($response as $item) {
$post = Facebook::get($item['id'])->getGraphObject();
}

From v2.4 on, you have to specify each field you want to have returned from the Grpah API. You should be able to use
/elbeko/feed?fields=id,message,link,attachments{media}&limit=25
which returns something like
{
"data": [
{
"id": "1472127519670095_1645415342341311",
"message": "Benieuwd naar ons nieuw project in de regio Gent? Binnenkort meer info via www.elbeko.be!",
"link": "https://www.facebook.com/elbeko/photos/a.1589552831260896.1073741835.1472127519670095/1645415342341311/?type=3",
"attachments": {
"data": [
{
"media": {
"image": {
"height": 405,
"src": "https://scontent.xx.fbcdn.net/hphotos-xpl1/v/t1.0-9/s720x720/11954813_1645415342341311_5204470874884096944_n.jpg?oh=0a7e10b12d3feb90b2de79fa60a7f8f8&oe=56C30337",
"width": 720
}
}
}
]
}
}
],
"paging": {
"previous": "https://graph.facebook.com/v2.5/1472127519670095/feed?fields=id,message,link,attachments%7Bmedia%7D&limit=2&format=json&since=1441790055&access_token=&__paging_token=&__previous=1",
"next": "https://graph.facebook.com/v2.5/1472127519670095/feed?fields=id,message,link,attachments%7Bmedia%7D&limit=2&format=json&access_token=&until=1441124382&__paging_token="
},
}
See
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5#fields
https://developers.facebook.com/docs/apps/upgrading#v23tov24
https://developers.facebook.com/docs/graph-api/reference/v2.5/post
In the past, responses from Graph API calls returned a set of default fields. In order to reduce payload size and improve latency on mobile networks we have reduced the number of default fields returned for most Graph API calls. In v2.4 you will need to declaratively list the response fields for your calls.

Related

Wordpress Shortcode to get Youtube Channel Views via YouTube API

I am Wordpress User and not the Programmer.
I want to create a shortcode in the functions.php to get the total views of any youtube channel.
I use the shortcode and give the channel ID Value. After the shortcode function gives back via YouTube API the current Channel Views of the given Channel ID.
In the YouTube Documentation, I found the following Code:
function channelsListById($service, $part, $params) {
$params = array_filter($params);
$response = $service->channels->listChannels(
$part,
$params
);
print_r($response);
}
channelsListById($service,
'statistics',
array('id' => 'UC_x5XG1OV2P6uZZ5FSM9Ttw'));
}
The Response looks like that:
/**
* API response
*/
{
"kind": "youtube#channelListResponse",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/d9WwK8U1FuV6c-shZpzmnYUgGvQ\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#channel",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/Psv3E9LCdk66gkF4HSIYK44crMI\"",
"id": "UC_x5XG1OV2P6uZZ5FSM9Ttw",
"statistics": {
"viewCount": "118283693",
"commentCount": "393",
"subscriberCount": "1165598",
"hiddenSubscriberCount": false,
"videoCount": "4435"
}
}
]
}
I think, I need to add some code to get the item field "viewCount" Value.
I don´t know if it is possible to do that, but I hope some of you Pros know the solution.
At the End of the Code, I just have to add:
add_shortcode( 'channel_views', 'shortcode_title' );
Would be amazing, if you could respond with the code I have to invert to the functions.php ;)
If you need more information to be able to respond, just ask me.
Use this plugin.
https://wordpress.org/plugins/youmax-channel-embeds-for-youtube-businesses/
This plugin simply gets all the videos from youtube channel in the grid format.

Can't access object variables in PHP from Facebook Graph API

I have a request to the Facebook Graph API that returns a post from a facebook account.
I am simply trying to access the data instance of the object returned in PHP but everything I have tried returns NULL.
Sample response
{
"data": [
{
"id": " 111111111111111111100000",
"message": "Coming soon #PERFECTFIT 05.07.17 👀\nRegister to be one of the first to find out what it is here ⬇️\nhttp://www.bathrugby.com/the-club/supporters/perfect-fit-register/",
"created_time": "2017-06-26T17:39:20+0000",
"link": "http://www.bathrugby.com/the-club/supporters/perfect-fit-register/",
"full_picture": "https://scontent.xx.fbcdn.net/v/t39.2147-6/19284954_1592534984092755_4946207882807869440_n.jpg?oh=56cc96435f423cec31962966b6f689c2&oe=59DB08B6"
}
]
}
I want to get at the array of objects data provides so I can MVC the data returned in a larger response.
This doesn't currently work:
$response->data; // returns null
$response[0]->data; // returns null
$response->data[0]; // returns null
Feel I'm missing something obvious.
first decode it with json_decode then trying to access the data object
$res = json_decode($response);
print_r($res->data);
print_r($res->data[0]->id);
print_r($res->data[0]->message);
code
you have to use first json decode function so you can get the data from the json response.
$response = '{
"data": [
{
"id": "143384725674462_1592535354092718",
"message": "Coming soon #PERFECTFIT 05.07.17 👀\nRegister to be one of the first to find out what it is here ⬇️\nhttp://www.bathrugby.com/the-club/supporters/perfect-fit-register/",
"created_time": "2017-06-26T17:39:20+0000",
"link": "http://www.bathrugby.com/the-club/supporters/perfect-fit-register/",
"full_picture": "https://scontent.xx.fbcdn.net/v/t39.2147-6/19284954_1592534984092755_4946207882807869440_n.jpg?oh=56cc96435f423cec31962966b6f689c2&oe=59DB08B6"
}
]
}';
$response = json_decode($response);
print_r($response->data[0]->id);
print_r($response->data[0]->message);

Parsing FB Graph API data Cleanly

I am currently working to parse through FB api data. Take the following for example...
{
"data": [
{
"id": "ID",
"name": "Creative!"
},
{
"id": "ID",
"name": "a name"
}
],
"paging": {
"cursors": {
"after": "fdsagfsganhdfs==",
"before": "gfdwiolrukjhteqrfgbh"
}
},
"summary": {
"total_count": 2
}
}
This is an example of what is returned when the Graph API is queried for likes. The issue I am having is that I want a clean way to get the total_count out of this data. Often times it will come in without the summary field if there are no 'likes'. This is easily parsed by doing a few if isset() and if array_key_exists but I will be dealing with a lot of data and this use case applies to many different type of data from FB. Any advice on just getting the total_count field? FQL would work but seems to be deprecated. Thanks.
If the total_count field is in fact the sum of the number of elements in the data array then you can simply use the count function in PHP to count the length of the array.
$json_array = json_decode($fb_json_string, true);
$total_count = (isset($json_array['total_count']))? $json_array['total_count']:((isset($json_array['summary']['total_count']))? $json_array['summary']['total_count']:0);
I don't know if I can make it any uglier, it's simply a suggestion though.

How can i fetch page data from graph with only pageid

I have a problem with graph api.
The call "https://graph.facebook.com/[pageID]" fails with the message
{
"error": {
"message": "Unsupported get request.",
"type": "GraphMethodException",
"code": 100
}
}
I need the full url of the facebook-page. Is there a way to get the url of a page from pageid only?
EDIT: Thank you for tips. The error was, that when the page is not public, that i cannot query infos via graph. Its also not working, when i (admin of page!) call the graph-url...
If you take a look at Page Graph API Reference there is a example url to get information about the page, include the url of the page that you want. The example url request is :
https://graph.facebook.com/19292868552
and the result is :
{
"about": "Grow your app with Facebook\nhttps://developers.facebook.com/ ",
"company_overview": "Facebook Platform enables anyone to build social apps on Facebook, mobile, and the web.\n\n",
"is_published": true,
"talking_about_count": 34563,
"username": "FacebookDevelopers",
"website": "http://developers.facebook.com",
"were_here_count": 0,
"category": "Product/service",
"id": "19292868552",
"name": "Facebook Developers",
"link": "http://www.facebook.com/FacebookDevelopers",
"likes": 1187896,
"cover": {
"cover_id": "10151298218353553",
"source": "http://sphotos-b.ak.fbcdn.net/hphotos-ak-ash4/s720x720/377655_10151298218353553_500025775_n.png",
"offset_y": 0,
"offset_x": 0
}
}
I think in your problem you put the wrong page ID.

Looking for effiicient way to pull data from facebook graph api

I've been recently working with facebook graph api , when I'm pulling user's recents status messages using https://graph.facebook.com/me/statuses?access_token=... all I get is a long list which contains details about the status content, and I want to pull the names of people who liked the status.
The current solution I've been thinking about is using preg match,
I'd like to know if you guys have any other suggestions,
Thanks in advance
I'm not sure what path you're using, but you can fetch the list of users who like a particular status update by requesting the likes connection on an update.
For example, requesting https://graph.facebook.com/10150710880081168/likes yields something like the following for me:
{
"data": [
{
"id": "276700623",
"name": "Vikki Carter"
},
{
"id": "1514365200",
"name": "Darren Bean"
},
{
"id": "539760281",
"name": "Ian Sidaway"
}
],
"paging": {
"next": "https://graph.facebook.com/10150710880081168/likes?format=json&limit=25&offset=25&__after_id=539760281"
}
}
You'll need the ID of the status update, but also the user_status permission from the logged-in user.
EDIT: Iterating over the returned data and assigned users into an array:
<?php
$result = $facebook->api('[status_id]/likes');
$names = array();
foreach ($result['data'] as $user) {
$names[] = $user['name'];
}
You should now have an array of names of users who liked the status in question.

Categories