How do i grab the latest status update from a facebook page in php? Not a users profile but a page (that you can like)? The documentation is a nightmare. Having trouble finding an answer to this!
Pages are just the same as profiles in terms of accessing them though the graph. To get the wall posts from a page you use the url https://graph.facebook.com/PAGE_ID/feed.
Adding a limit parameter will return only the most recent: https://graph.facebook.com/PAGE_ID/feed?limit=1.
Here's a quick and dirty example of how this would work in PHP:
try {
$feed = $facebook->api('/PAGE_ID/feed?limit=1');
} catch (FacebookApiException $e) {
error_log($e);
}
Related
I'm using a Facebook application to create a kind of "social sharing" platform.
The website version uses Facebook API, Twitter OAuth and Instagram, and the canvas version only uses Facebook API.
I know that, when using the Canvas, Facebook sends a signed_request parameter, which actually recognizes that I'm using the Canvas version and not the website version. So, this is my login code:
login.php
$is_canvas=$SController->checkIsCanvas();
if(isset($_REQUEST['request_ids'])) {
$_SESSION['auth_request']=strip_tags($_GET['request_ids']);
}
socialclass.php
function checkIsCanvas() {
$helper = new FacebookCanvasLoginHelper();
try {
$session = $helper->getSession();
} catch (FacebookRequestException $ex) {
return $ex->getMessage();
} catch (\Exception $ex) {
return $ex->getMessage();
}
if ($session) {
$_SESSION['is_canvas']='true';
$accessToken=$session->getAccessToken();
$_SESSION['auth_tk_facebook']=$accessToken->__toString();
$request = new FacebookRequest(
$session,
'GET',
'/me?fields=id,name,first_name,last_name,birthday,email,age_range,location,gender'
);
$response = $request->execute();
$userProfile = $response->getGraphObject(GraphUser::className());
if(isset($userProfile)) {
$login=$this->checkFacebookLogin($userProfile);
if($login===false) {
return $userProfile;
} else {
header("Location:".getBASEURL());
}
} else {
return false;
}
} else {
return false;
}
}
The problem is that when I logout from canvas and then re-login, Facebook hasn't sent any signed_request parameter, so I can't recognize if I'm actually using the Canvas version or the website's one. To temporally solve this, I just reload the page, so Facebook can save another signed_request, but it's quite annoying.
This problem comes up also when i go from the Canvas version to the website version without logging-out. Obviously, the is_canvas session variable doesn't unset, so the website version appears equal to the Canvas version.
How can I always check if I'm in the canvas, or in the website without reloading after logging-out? The problem should be, I hope, easy to solve, but I've not figured out how to do that yet.
If you have any questions about more pieces of code or if I wasn't as clear as I'm expecting ( sorry for my English, too ) don't worry about asking :)
Thanks in advice.
Similar/related Question is solved here : Check if site is inside iframe
Because canvas is always called in iframe. Please check if it helps.
I am the administrator of a Facebook page which needs to have an age restriction of 17+ on it.
On my website, I am making some Graph API queries, like querying photo albums and linking to them on Facebook. I am using the PHP Facebook API.
I can't seem to get to grips around the page access tokens and when they expire and when not.
I tried to follow this answer here, which was highly voted, but it isn't clear what you do with the first access_token after you copy it. I am not sure if this is even valid any more.
I tried to access the page using the access token I get when I do me/accounts in Graph Explorer and it works, but after some time it expires and I get an exception that the token expired. I need something that stays working and doesn't expire and need me to go in and update it.
This is the code I have so far:
$fbconfig = array();
$fbconfig['appId'] = DEF_APP_ID;
$fbconfig['secret'] = DEF_APP_SECRET;
$fbconfig['fileUpload'] = false;
Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;
$facebook = new Facebook($fbconfig);
//I am not sure if the following is the right way
$facebook->setAccessToken(DEF_ACCESS_TOKEN);
$facebook->setExtendedAccessToken();
...
try
{
$albums_resp = $facebook->api('/'.$id.'/albums','GET');
...
}
catch (FacebookApiException $ex)
{
error_log($ex);
}
What is the right way to achieve this?
Im creating a Facebook application which will ask to like page before continue with the app. But after changing Facebook api platform to version 2, I cant get Facebook user id, I get scoped user id instead. Using scoped_user_id i cant get users who liked my page. Heres my code
if ($fb_scope_id){
try {
$user_like = $facebook->api('/'.$fb_scope_id.'/likes/');
} catch (FacebookApiException $e) {
$user_like = null;
}
}
It returns an empty array. Any solution?
I am building a website where I need to retrieve Facebook shares and likes of numerous links and URLs from different sites.
The problem is, for some URLs it is impossible to get what I wanted. For example, when I look for data about links that look like http://www.example.com/?a=1&b=2&c=3 all I get is wrong data about http://www.example.com/?a=1 and the rest of the URL (&b=2&c=3) is simply ignored by Facebook.
Here at StackOverflow, a lot of people are looking for an answer and many questions are simply unanswered. So, once I did it right, I'm back to tell how I did it.
P.S. : This works only for non-Facebook URLs. If you're looking for shares and likes counts of an internal Facebook link (image, video ...), this won't work for you. I'll be using PHP to answer the question.
To get likes and shares counts, I use FQL instead of the Graph API (even though I am actually using the Graph API to send the query).
But this is not enough : to be able to do so, I had call the rawurlencode() function on the URL I want to get data about. Otherwise, I'll keep getting errors.
So, this likes(), the function I am using to have the counts :
function likes($url) {
$url = rawurlencode($url);
$json_string = file_get_contents("http://graph.facebook.com/fql?format=json&q=SELECT%20share_count,%20like_count%20FROM%20link_stat%20WHERE%20url='$url'");
$json = json_decode($json_string, true);
if (key_exists("data", $json)) {
if (is_array($json["data"])) {
if (array_key_exists("0", $json["data"])) {
return intval($json["data"]["0"]["share_count"]) + intval($json["data"]["0"]["like_count"]);
} else {
echo "Error : '0' is no key<br/>";
return 0;
}
} else {
echo "Error : data is no table <br/>";
return 0;
}
} else {
echo "Error : No data key <br/>";
return 0;
}
}
I hope this will help someone someday :)
I know, this question was asked hundred times, but none of the given solutions works for me.
I'm trying to post on the wall of every page the current user owns. So, I first get the permissions to publish_actions, read_stream, manage_pages and offline_access. Then I check via /me/permissions, that the user really granted those permissions.
Then, I try to get all pages of the user and post on the wall of the page, if the user is the admin:
$accounts = $facebook->api("/me/accounts", "GET", array("access_token"=>$facebook->getAccessToken()));
$data = $accounts["data"];
foreach ($data as $page)
{
if (isset($page["perms"]) && in_array("CREATE_CONTENT", $page["perms"])))
{
$facebook->api("/".$page["id"]."/feed", "POST", array("link"=>$link, "access_token"=>$page["access_token"]));
}
}
But, posting on the wall of the page fails, with the well known error message
Uncaught OAuthException: (#200) The user hasn't authorized the application to perform this action
Any ideas? Where's my fault?
If you can make the same call via the Graph API Explorer or cURL for one of the pages then your logic is right. So I would place a try/catch on the page/id call to see if a specific page is messing up the loop.
try {
$facebook->api("/".$page["id"]."/feed", "POST", array("link"=>$link, "access_token"=>$page["access_token"]));
} catch (FacebookApiException $e) {
error_log($e . " for: " .$page["id"] );
}
}