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
Related
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';
}
*/
?>
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
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?
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.
<?php
// include required files form Facebook SDK
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookCanvasLoginHelper.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/FacebookPermissionException.php' );
require_once( 'Facebook/FacebookOtherException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphSessionInfo.php' );
require_once( 'Facebook/GraphUser.php' );
require_once( 'Facebook/GraphLocation.php' );
include ("fns.php");
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookCanvasLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookOtherException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphSessionInfo;
use Facebook\GraphUser;
use Facebook\GraphLocation;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( 'XXXXXXXXXXx','yyyyyyyyyyyyyyyy' );
$helper = new FacebookCanvasLoginHelper();
try {
$session = $helper->getSession();
} catch (Exception $ex) {
// echo $ex->getMessage();
}
if($session)
{
$request = new FacebookRequest( $session, 'GET', '/me' );
try{
$response = $request->execute();
$graphObject = $response->getGraphObject();
//echo print_r( $graphObject, 1 );
$fid = $graphObject->getProperty('id'); echo $fid;
$femail = $graphObject->getProperty('email'); echo $femail;
$ffirst_name = $graphObject->getProperty('first_name'); echo $ffirst_name;
} catch (Exception $ex) {
// echo $ex->getMessage();
}
}
?>
First off, I am not an expert PHP coder, so I do not understand a lot of things, but I do know how to use a working example. I made a Canvas App that was working fine. Then I installed ssl and it no longer works. The code itself is a mess from my efforts to fix the problem.
Can I just get a working example that echos user public data using FacebookCanvasLoginHelper with the latest 4.06 PHP SDK? I can take it up from there if I can see that.
Since V4.0, getsession() no longer seems to work. In V4.06 its supposed to have been fixed, but I still cant get it to work.
Thanks in advance
Edit
I have added the relevant code. After debugging, i find that it appears $session is not valid or something because code execution never gets past "$request = new FacebookRequest( $session, 'GET', '/me' );". I cant seem to get anything echoed out after that. please any help is much appreciated.
If it worked previously and you ONLY changed to SSL then the only thing that would cause the error and lack of session is a redirect mismatch.
Your old app redirect would be http and the new app redirect will be https
Change the Facebook app to reflect this change in the app redirect set up.
php 5.4 is required.
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /var/www/Facebook/FacebookResponse.php on line 137