Get page feeds from facebook page - php

I am using facebook api for getting feeds from facebook page.
Here is the code I am using:
session_start();
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once ('Facebook/GraphObject.php' );
require_once ('Facebook/GraphSessionInfo.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once('Facebook/HttpClients/FacebookHttpable.php');
require_once( 'Facebook/HttpClients/FacebookCurl.php');
require_once ( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
FacebookSession::setDefaultApplication('app_id', 'app_secret');
$session = new FacebookSession('access_token');
$session = FacebookSession::newAppSession();
// To validate the session:
try {
$session->validate();
} catch (FacebookRequestException $ex) {
// Session not valid, Graph API returned an exception with the reason.
echo $ex->getMessage();
}
$request = new FacebookRequest(
$session,
'GET',
'/{page-id}/feed'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
I have added app_id, app_secret,page-id and access_token in the code. But I am getting the error
Fatal error: Uncaught exception 'Facebook\FacebookAuthorizationException' with message '(#803) Some of the aliases you requested do not exist: {page-id}
But if I open https://graph.facebook.com/page-id?access_token=my_access_token, I get the required data.
I am doing all the stuff on localhost. Can some one help me finding the error?

Related

How to get /insights/page_fans_country/ with PHP?

I've been really desperate with this problem, I did not find an earlier post/solution for it.
So, the goal is to connect to Facebook (App created, token created, etc.) using PHP and access the /insights/page_fans_country/ of a page. I am able to access my own data with /me, but the access token is wrong for the session and therefore the page_fans_country (that is a public metric) cannot be accessed.
My code is here:
require_once( 'lib/src/Facebook/FacebookSession.php');
require_once( 'lib/src/Facebook/FacebookRequest.php' );
require_once( 'lib/src/Facebook/FacebookResponse.php' );
require_once( 'lib/src/Facebook/FacebookSDKException.php' );
require_once( 'lib/src/Facebook/FacebookRequestException.php' );
require_once( 'lib/src/Facebook/FacebookRedirectLoginHelper.php');
require_once( 'lib/src/Facebook/FacebookAuthorizationException.php' );
require_once( 'lib/src/Facebook/GraphObject.php' );
require_once( 'lib/src/Facebook/GraphUser.php' );
require_once( 'lib/src/Facebook/GraphLocation.php' );
require_once( 'lib/src/Facebook/GraphAlbum.php' );
require_once( 'lib/src/Facebook/GraphPage.php' );
require_once( 'lib/src/Facebook/GraphUserPage.php' );
require_once( 'lib/src/Facebook/GraphSessionInfo.php' );
require_once( 'lib/src/Facebook/Entities/AccessToken.php');
require_once( 'lib/src/Facebook/HttpClients/FacebookCurl.php' );
require_once( 'lib/src/Facebook/HttpClients/FacebookHttpable.php');
require_once( 'lib/src/Facebook/HttpClients/FacebookCurlHttpClient.php');
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphLocation;
use Facebook\GraphAlbum;
use Facebook\GraphPage;
use Facebook\GraphUserPage;
use Facebook\GraphSessionInfo;
use Facebook\AccessToken;
use Facebook\FacebookHttpable;
use Facebook\FacebookCurlHttpClient;
use Facebook\FacebookCurl;
session_start();
$app_id = 'MY_ID';
$app_secret = 'MY_SECRET';
$redirect_url='http://localhost/facebook/index.php/';
$access_token = ('MY_ACCESS_TOKEN');
FacebookSession::setDefaultApplication($app_id,$app_secret);
$helper = new FacebookRedirectLoginHelper($redirect_url);
$sess = $helper->getSessionFromRedirect();
if(isset($sess)){
$request = new FacebookRequest($sess, 'GET', '/me'); // THIS WORKS, GIVES MY PERSONAL INFORMATION!
// $request = new FacebookRequest($sess, 'GET', '/81221197163/insights/page_fans_country/');
$response = $request->execute();
$graphObject = $response->getGraphObject();
print_r($graphObject);
}else{
//else echo login
echo '<a href='.$helper->getLoginUrl().'>Login with facebook</a>';
}
So basically the commented request $request = new FacebookRequest($sess, 'GET', '/81221197163/insights/page_fans_country/'); does not work (of course while it is not commented).
So my questions are:
Am I doing something wrong?
Is there a command to change the session's Access Token?
What is the best way to get the /insights/page_fans_country/ values with PHP?
Thank you in advance for every reply!
Roni

Facebook SDK V4 get name

For example, I want to retrieve my name using Facebook PHP SDK 4.
For that I use this code
<?php
require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/Entities/SignedRequest.php' );
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookOtherException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\Entities\AccessToken;
use Facebook\Entities\SignedRequest;
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
FacebookSession::setDefaultApplication('app_id', 'app_secret');
// If you already have a valid access token:
$session = new FacebookSession('access token');
// To validate the session:
try {
$session->validate();
} catch (FacebookRequestException $ex) {
// Session not valid, Graph API returned an exception with the reason.
echo $ex->getMessage();
} catch (\Exception $ex) {
// Graph API returned info, but it may mismatch the current app or have expired.
echo $ex->getMessage();
}
try {
$response = (new FacebookRequest($session, 'GET', '/me'))->execute();
$object = $response->getGraphObject();
echo $object->getProperty('name');
} catch (FacebookRequestException $ex) {
echo $ex->getMessage();
} catch (\Exception $ex) {
echo $ex->getMessage();
}
?>
I get my access token from this link :
https://graph.facebook.com/oauth/access_token?client_id={app-id} &client_secret={app-secret}&grant_type=client_credentials
When I execute the code I get this error :
An active access token must be used to query information about the
current user.
What i'm doing wrong ? Thanks

facebook PHP sdk cannot login

I have been trying to use facebook's PHP sdk to login.
Following is one of my codes.
<?php
session_start();
require_once( 'lib/Facebook/FacebookSession.php' );
require_once( 'lib/Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'lib/Facebook/FacebookRequest.php' );
require_once( 'lib/Facebook/FacebookResponse.php' );
require_once( 'lib/Facebook/FacebookSDKException.php' );
require_once( 'lib/Facebook/FacebookRequestException.php' );
require_once( 'lib/Facebook/FacebookAuthorizationException.php' );
require_once( 'lib/Facebook/GraphObject.php' );
require_once('lib/Facebook/GraphUser.php');
require_once('lib/Facebook/GraphSessionInfo.php');
require_once( 'lib/Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'lib/Facebook/HttpClients/FacebookCurl.php' );
require_once( 'lib/Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'lib/Facebook/Entities/AccessToken.php' );
require_once( 'lib/Facebook/Entities/SignedRequest.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
// Initialize application by Application ID and Secret
FacebookSession::setDefaultApplication('app_id','app_secret');
// Login Healper with reditect URI
$helper = new FacebookRedirectLoginHelper( 'http://124.125.244.163/rtCamp/home.php' );
try {
$session = $helper->getSessionFromRedirect();
}
catch( FacebookRequestException $ex ) {
// Exception
}
catch( Exception $ex ) {
// When validation fails or other local issues
}
// Checking Session
if(isset($session))
{
// Request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// Responce
$data = $response->getGraphObject();
// Print data
echo print_r( $data, 1 );
}
else
{
// Login URL if session not found
echo 'Login';
}
?>
Now the problem I am facing is that there is some problem with the line where I can get a session.
i.e $session = $helper->getSessionFromRedirect() after the page redirects from facebook.
Now I even implemented this guy's entire github dir with the same login code here
still, my code stops working after that line and there is not a single error displayed. I tried numerous solutions none of them work. I downloaded the stable Facebook PHP SDK 4.0.0 and I am running a home LAMP server with PHP 5.4. I am stuck.
UPDATE: ERROR comes in Apace log
PHP Fatal error: Class
'Facebook\HttpClients\FacebookStreamHttpClient' not found in
/var/www/rtCamp/lib/Facebook/FacebookRequest.php on line 166, referer:
http://124.125.244.163/rtCamp/home.php
<?php
require_once( 'lib/Facebook/FacebookSession.php' );
require_once( 'lib/Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'lib/Facebook/FacebookRequest.php' );
require_once( 'lib/Facebook/FacebookResponse.php' );
require_once( 'lib/Facebook/FacebookSDKException.php' );
require_once( 'lib/Facebook/FacebookRequestException.php' );
require_once( 'lib/Facebook/FacebookAuthorizationException.php' );
require_once( 'lib/Facebook/GraphObject.php' );
require_once('lib/Facebook/GraphUser.php');
require_once('lib/Facebook/GraphSessionInfo.php');
require_once( 'lib/Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'lib/Facebook/HttpClients/FacebookCurl.php' );
require_once( 'lib/Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'lib/Facebook/Entities/AccessToken.php' );
require_once( 'lib/Facebook/Entities/SignedRequest.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
session_start();
$app_id='';
$app_secret='';
$redirect_url='http://124.125.244.163/rtCamp/home.php';
// Initialize application by Application ID and Secret
FacebookSession::setDefaultApplication($app_id,$app_secret);
// Login Healper with reditect URI
$helper = new FacebookRedirectLoginHelper($redirect_url);
$session = $helper->getSessionFromRedirect();
// Checking Session
if(isset($session))
{
// Request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// Responce
$data = $response->getGraphObject(GraphUser::classname());
$name=$graph->getName();
echo "hi $name";
// Print data
echo print_r( $data, 1 );
}
else
{
// Login URL if session not found
echo 'Login';
}
/*
try {
$session = $helper->getSessionFromRedirect();
}
catch( FacebookRequestException $ex ) {
// Exception
}
catch( Exception $ex ) {
// When validation fails or other local issues
}
// Checking Session
if(isset($session))
{
// Request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// Responce
$data = $response->getGraphObject();
// Print data
echo print_r( $data, 1 );
}
else
{
// Login URL if session not found
echo 'Login';
}
*/
?>

Facebook login PHP sdk 4.4

I have been trying to test the Facebook PHP login system but keep getting the error:
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /xxxxx/httpd.www/Facebook/FacebookRedirectLoginHelper.php on line 146
I have read through several different examples and tried to implement each after first running into this as well as read through the different questions/answers on here all to no avail.
If anyone has ran into this and found a solution or knows of the solution i would much appreciate the pointer.
Edit current code added.
// init app with app id and secret
FacebookSession::setDefaultApplication( 'xxx','xxx' );
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://somesite.com/index.php' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
// print data
echo print_r( $graphObject, 1 );
} else {
// show login url
echo 'Login';
}
I got the same question with you.you can not use the "include" like in old version. In v4.4,they use it like this:
define('FACEBOOK_SDK_V4_SRC_DIR', '/path/to/facebook-php-sdk-v4/src/Facebook/');
require __DIR__ . '/path/to/facebook-php-sdk-v4/autoload.php';
and then ,just the same as you
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
That is all!
reference:Facebook SDK for PHP

Facebook session dissappears when refreshing the webpage

I'm new to the facebook login SDK for PHP and i'm trying to create a "log in with facebook" function on my website.
I'm using this code which works great:
session_start();
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\FacebookCurl;
use Facebook\FacebookCurlHttpClient;
use Facebook\FacebookHttpable;
FacebookSession::setDefaultApplication('app-id', 'app-secret');
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'app-redirect-link' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
// print data
echo print_r( $graphObject, 1 );
} else {
// show login url
echo 'Login';
}
If I press "Login" I get my details and everything I need, but as soon as i'm refreshing the page - the "Login" link appears again, as if the session does not exist anymore.
Any help would be appreciated.
Defining a new session and using it helped.

Categories