I am trying to pull a public page from facebook using php and json to sort the data received. I have everything working perfectly apart from replies on comments on posts. I can get all the list of comments on a post but the replies to that comment do not appear on the json data that is returned to me. I believe when the user of the page comments on his own post the story tag is given to the comment and it is unrelated to the comments section of that post. But if anybody else replies to a comment i cannot see their message anywhere. Is there any way to try and get the replies of comments returned in json along with everything else?
edit: found a better solution
see the answer here: How to get the image from a facebook photo comment?
the query you want to perform is
/{page-post-id}/comments?fields=from,message,id,attachment,created_time,comments.fields(from,message,id,attachment,created_time)
to query this via PHP you would do something like this:
$page_comment_id = 12345; // put your id here
$access_token = ''; // put your facebook access token here
$url = 'https://graph.facebook.com/' . $page_comment_id . '/comments?fields=from,message,id,attachment,created_time,comments.fields(from,message,id,attachment,created_time)&access_token=' . $access_token;
$data = json_decode(file_get_contents($url),true);
print_r($data);
Related
i want to make a social media audit tool and i want to get likes of a person's Fb page. Basically what i want is i want that person to enter his FB pages URL and then i want to fetch the likes of his page and echo it in my PHP page. Is there any way I can do that without using graph API or any API. I just want a simple piece of code.
I have searched many questions regarding my project on web and on StackOverflow as well but coudn't find what i wanted, at last, I am asking this question. Is there anyone who can help me regarding this?
Thanx in advance.
<?php
$file = "https://www.facebook.com/IntellectualIndies/?epa=SEARCH_BOX";
$data = file_get_contents($file);
preg_match_all ('~<div class=\'_4bl9\'>\s*(<div.*?</div>\s*)?(.*?)</div>~is', $data, $matches);
$content = $matches[1];
$total = count($content);
for($i=0; $i<$total; $i++){
echo $content[$i]."<br />";
}
?>
i tried this code.
Is there any way I can do that without using graph API or any API
No, scraping is not allowed on Facebook.
There is only one way to do this:
Apply for Page Public Content Access
Use the Graph API with the following endpoint/field: /page-id?fields=fan_count
API Reference with example code: https://developers.facebook.com/docs/graph-api/reference/page/
I'm using PHP to build an application that gets some statistics from individual Instagram accounts.
I do a get request to the following URL to get a JSON formatted string:
https://www.instagram.com/{username}/?__a=1
With json_decode, I get the followers and the followings by:
$follows = $data['user']['follows']['count'];
$followedBy = $data['user']['followed_by']['count'];
But I don't know how to get the posts count.
For example for NASA profile:
https://www.instagram.com/nasa/
... I need the number (currently) 1.967.
Any idea?
And a final question: do I have to use API Key to get the stats, or the GET to the above URL is enough?
what is wrong with
$json = file_get_contents('https://www.instagram.com/nasa/?__a=1');
$obj = json_decode($json);
echo $obj->user->media->count;
it just spit out the correct post count as you wanted?
As of now (2020) Instagram has made some changes to the JSON response and the code below does the same as the accepted answer above, also with formatted output.
$ig = json_decode(file_get_contents('https://www.instagram.com/nasa/?__a=1'));
echo number_format($ig->graphql->user->edge_owner_to_timeline_media->count);
I'm curious to use the facebook graph API as I don't like the simple like button with number of likes next to it. However I'm finding their documentation a bit confusing. I want to do this via PHP so I'm assuming I need the PHP SDK, is that right?
I'm looking at this page Graph API: Page however I don't understand how I would call the likes.
I'm not asking for someone to code this for me, just a little help in the right direction to understand how it works :)
Try this:
$json = #file_get_contents("http://graph.facebook.com/yourpage");
$json1 = json_decode($json,true);
$likes = $json1['likes'];
echo $likes;
This is pretty straightforward. Using the ID or the username of the page, access that
node via the Graph API:
PAGE_ID?fields=likes
In PHP would be something like:
$token_string = file_get_contents('https://graph.facebook.com/oauth/access_token?client_id='.$appID.'&client_secret='.$app_secret.'&grant_type=client_credentials');
$likes_response = file_get_contents('https://graph.facebook.com/PAGE_ID?fields=likes&'.$token_string);
$likes_obj = json_decode($likes_response);
$likes = $likes_obj->likes;
echo 'Likes count for page: ' . $likes;
Hy well how does the url looks when you use a PageID and at the same time want the count of all comments and likes back I tried it hard way
$feed_profile = $facebook->api('/'.$row[0].'/feed&access_token=$access_token&limit=500',"get");
$post_profile = $facebook->api('/'.$row[0].'/comments?access_token=$access_token&limit=500',"get");
also tried comments& instead of comments?
I dont know how to get back the comments and likes what must I exactly do my var $row[0] stores various Pages from fb
the feed has connections EG: comments, likes that provide the count.
$feed_profile = $facebook->api('/'.$row[0].'/feed&access_token=$access_token&limit=5',"get");
// get comment/like count for first post in array. this can be done with a loop.
echo 'Comments: '.$feed_profile[data][0][comments][count].'<br />';
echo 'Likes: '.$feed_profile[data][0][likes][count].'';
NOTE: you will have to account for error handling, when no comments or
likes have been made the connections arrays do not exsist
In Short, I am pulling the feed from my blogger using the Zend API in PHP. I need to get the URL that will link to that post in blogger. What is the order of functions I need to call to get that URL.
Right now I am pulling the data using:
$query = new Zend_Gdata_Query('http://www.blogger.com/feeds/MYID/posts/default');
$query->setParam('max-results', "1");
$feed = $gdClient->getFeed($query);
$newestPost = $feed->entry[0];
I can not for the life of me figure out where I have to go from here to get the URL. I can successfully get the Post title using: $newestPost->getTitle() and I can get the body by using $newestPost->getContent()->getText(). I have tried a lot of function calls, even ones in the documentation and most of them error out. I have printed out the entire object to look through it and I can find the data I want (so I know it is there) but the object is too complex to be able to just look at and see what I have to do to get to that data.
If anyone can help me or at least point me to a good explanation of how that Object is organized and how to get to each sub object within it, that would be greatly appreciated.
EDIT: Never mind I figured it out.
You are almost there, really all you need to do is once you have your feed entry is access the link element inside. I like pretty URLs so I went with the alternate rather than the self entry in the atom feed.
$link = $entry->link[4]->href;
where $entry is the entry that you are setting from the feed.
The solution is:
$query = new Zend_Gdata_Query('http://www.blogger.com/feeds/MyID/posts/default');
$query->setParam('max-results', "1");
$feed = $gdClient->getFeed($query);
$newestPost = $feed->entry[0];
$body = $newestPost->getContent()->getText();
$body now contains the post contents of the latest post (or entry[0]) from the feed. This is just the contents of the body of the post, not the title or any other data or formatting.