I am currently logging a user in and then getting /me like this,
$graphObject = (new FacebookRequest( $session, 'GET', '/me/tagged_places' ))->execute()->getGraphObject()->asArray();
Then I am printing the data like this,
print_r( $graphObject, 1 )
If I want to get all the data for all the permissions I am asking for how can I do so then call on it when I need it?
When I change /me to /me/tagged_places etc. it works for that individual permission. How can I get them all?
Also once I have it I would like to organize it. Can I put this all into an array and then call each one as needed? If so how?
Here is all of my code,
// include required files form Facebook SDK
// added in v4.0.5
require_once( 'facebook/src/Facebook/FacebookHttpable.php' );
require_once( 'facebook/src/Facebook/FacebookCurl.php' );
require_once( 'facebook/src/Facebook/FacebookCurlHttpClient.php' );
require_once( 'facebook/src/Facebook/FacebookSession.php' );
require_once( 'facebook/src/Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'facebook/src/Facebook/FacebookRequest.php' );
require_once( 'facebook/src/Facebook/FacebookResponse.php' );
require_once( 'facebook/src/Facebook/FacebookSDKException.php' );
require_once( 'facebook/src/Facebook/FacebookRequestException.php' );
require_once( 'facebook/src/Facebook/FacebookOtherException.php' );
require_once( 'facebook/src/Facebook/FacebookAuthorizationException.php' );
require_once( 'facebook/src/Facebook/GraphObject.php' );
require_once( 'facebook/src/Facebook/GraphSessionInfo.php' );
use Facebook\FacebookHttpable;
use Facebook\FacebookCurl;
use Facebook\FacebookCurlHttpClient;
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;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( 'foo','foo' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://www.website.com/' );
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
} else {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
// handle this better in production code
print_r( $ex );
} catch( Exception $ex ) {
// When validation fails or other local issues
// handle this better in production code
print_r( $ex );
}
}
// see if we have a session
if ( isset( $session ) ) {
// save the session
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
// graph api request for user data with response
$graphObject = (new FacebookRequest( $session, 'GET', '/me/tagged_places' ))->execute()->getGraphObject()->asArray();
}
?>
Not sure whether I understood you correctly, but if you want to query for more fields and connections use the so-called field expansion:
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.0#fields
For example:
/me?fields=id,first_name,last_name,tagged_places
Related
I started working with the Facebook Graph API and noticed that when trying to get all the member list from my group (that I own), echo-ed the user name and I get only (around) 4900 users as an output - I have 15K+ users in my group!!!
To test that I var_dump-ed every list and it gave me 127 arrays with the text "more elements..." so I concluded that echoing the list was good, but getting the lists was wrong.
I tried to get the members list using the Graph API Explorer and it gave the same results with wrong "next" links with strange offset and limit parameters that don't make any sense and that I didn't enter.
This is my code for now (scroll to "Cool Stuff Here"):
<?php
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/GraphUser.php' );
require_once( 'Facebook/GraphSessionInfo.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' );
use Facebook\GraphUser;
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\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
$app_id = '13434480350553256103140350';
$app_secret = 'a866dcd372123fc87373a345cvvf34d0e70c3cdf0vvc13db4521basdd99';
$redirect_url = 'http://localhost/test/YellowSpider/src/';
FacebookSession::setDefaultApplication( $app_id, $app_secret );
//helper for redirect
$helper = new FacebookRedirectLoginHelper($redirect_url);
// Requested permissions for the app - optional
$permissions = array(
'email',
'user_location',
'user_birthday',
'user_groups'
);
// Check if existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// Create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// Validate the access_token to make sure it's still valid
try {
if ( ! $session->validate() ) {
$session = null;
}
} catch ( Exception $ex ) {
// Catch any exceptions
$session = null;
}
} else {
// No session exists
try {
$session = $helper->getSessionFromRedirect(); ////Processes the redirect data from Facebook, if present. Returns a FacebookSession or null.
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
echo $ex->message;
}
}
// Check if a session exists
if ( isset( $session ) ) {
// Save the session
$_SESSION['fb_token'] = $session->getToken();
// Create session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
/********************************* Cool Stuff Here *****************************************/
$request = new FacebookRequest($session, 'GET', '/1501313558474010996619/members'); //Represents a request that will be made against the Graph API.
//From the response - get graph object
$response = $request->execute(); //Returns a Facebook\FacebookResponse from this request, from which a strongly-typed result can be retrieved.
//Throws an exception if the request fails. If the error is returned from Facebook,
//as opposed to a networking issue, a Facebook\FacebookRequestException is thrown.
$graphObject = $response->getGraphObject()->asArray();
var_dump($graphObject);
while($response){
var_dump($graphObject['data']);
//echo $members['id'];
if($response->getRequestForNextPage() != null){
$response = $response->getRequestForNextPage()->execute();
var_dump($response->getRequestForNextPage());
continue;
}else{
break;
}
}
/********************************* END Cool Stuff Here *****************************************/
// Create the logout URL (logout page should destroy the session)
$logoutURL = $helper->getLogoutUrl( $session, 'http://localhost/test/YellowSpider/src/' );
session_destroy(); // added this, clears all sessions.
echo 'Log out';
} else {
// No session
// Get login URL
$loginUrl = $helper->getLoginUrl( $permissions );
echo 'Log in';
}
What can be my problem? Is my code written ok? Might it be a temporary problem with Facebook?
With my experience with PHP, using GET and POST, GET is kinda limited when passing parameters while POST is not, But with the FB API I guess there isn't any connection to this because the POST method there is used for literally, post things on Facebook (?)
It's a known Facebook bug (for more than 2 years) in a low priority fix.
To bypass this I think scrapping is the only best option, nevertheless, https://www.facebook.com/apps/site_scraping_tos_terms.php
Before you all tell me there is already alot of tutorials on how to do this but their isn't any tutorials I foud on the brand new sdk so I am looking for some help.
I have just finished coding my facebook login but every time I login it doesn't ask for permission? And I was wondering how you ask for permission to post to their wall because I want to post on their wall saying they logged in every time they do
<?php
// include required files form Facebook SDK
require_once( 'fb/Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'fb/Facebook/HttpClients/FacebookCurl.php' );
require_once( 'fb/Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'fb/Facebook/Entities/AccessToken.php' );
require_once( 'fb/Facebook/Entities/SignedRequest.php' );
require_once( 'fb/Facebook/FacebookSession.php' );
require_once( 'fb/Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'fb/Facebook/FacebookRequest.php' );
require_once( 'fb/Facebook/FacebookResponse.php' );
require_once( 'fb/Facebook/FacebookSDKException.php' );
require_once( 'fb/Facebook/FacebookRequestException.php' );
require_once( 'fb/Facebook/FacebookOtherException.php' );
require_once( 'fb/Facebook/FacebookAuthorizationExcetion.php' );
require_once( 'fb/Facebook/GraphObject.php' );
require_once( 'fb/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;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( '798029503541633','e31a63ad8d46d3c5f4c9aea15af4b163' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://mywebsite.com/fblogin.php' );
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
}
if ( !isset( $session ) || $session === null ) {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
// handle this better in production code
print_r( $ex );
} catch( Exception $ex ) {
// When validation fails or other local issues
// handle this better in production code
print_r( $ex );
}
}
// see if we have a session
if ( isset( $session ) ) {
// save the session
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject()->asArray();
// print profile data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
// print logout url using session and redirect_uri (logout.php page should destroy the session)
echo 'Logout';
} else {
// show login url
echo 'Login';
}
This is my full code.
// include required files form Facebook SDK
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;
// start session
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication( 'KEY','SECRET' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'SITE_NAME.php' );
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
}
if ( !isset( $session ) || $session === null ) {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
// handle this better in production code
print_r( $ex );
} catch( Exception $ex ) {
// When validation fails or other local issues
// handle this better in production code
print_r( $ex );
}
}
// see if we have a session
if ( isset( $session ) ) {
// save the session
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '***/1458726944414455/likes***' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject()->asArray();
// print profile data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
// print logout url using session and redirect_uri (logout.php page should destroy the session)
echo 'Logout';
} else {
// show login url
echo 'Login';
}
In * * section above, if I put some Facebook page names or 'me', the code works fine. But when I insert '/1458726944414455/likes' something like this, I can't get any value so the results are always like this
Array ( )
What makes me mad is, /1458726944414455/likes this works fine in https://developers.facebook.com/tools/explorer, which is Graph API explorer. I think token scope is not that different in Graph API explorer and my own website.
What is wrong? I searched every website which wrote about facebook php sdk or graph api, but they are using almost same code with me.
Is this because that I didn't do anything about Review Submission Form? I just using the default App Status & Review. But I thought that it doesn't matter because I don't do login with Facebook something like that. What is wrong? Please help me.
Thank you for reading.
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.
i'm trying the new Facebook API (4.0) and I have serious problem with this.
The code of my page loginUser.php is:
<?php
require_once("autoload_fb.php");
// init app with app id and secret
FacebookSession::setDefaultApplication( '647345538685342','6ea4dc94874b31a637b6fe368bcfba76' );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://www.radiobrunoestate.mumbleserver.it/api/loginUser' );
// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
} else {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
echo $ex->message;
}
}
// see if we have a session
if ( isset( $session ) ) {
// save the session
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject()->asArray();
// print profile data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
// print logout url using session and redirect_uri (logout.php page should destroy the session)
echo 'Logout';
} else {
// show login url
echo 'Login';
}
?>
and the autoload_fb.php is simply:
<?php
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' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
?>
I get this error on first server:
Fatal error: Class 'FacebookSession' not found in /home/jack/provaprova/api/loginUser.php on line 6
Testing the same code on another Server i have this error:
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/eyikmdnu/public_html/jack/provaprova/api/Facebook/FacebookSession.php on line 140
Where could be the problem? could be a problem of the server configuration?
Thanks
Facebook PHP SDK v4 requires PHP 5.4. You are most likely using an older version of PHP.
I think you have to put the "use" statements also in the top of the "loginUser.php" file.
Suppose you've already made it work, but I think that was the problem.
Maybe this can help someone else.
Your use statements from the include file won't import them for the main file. So you'll need to move the use statements to your main file right after your include of the autoload_fb.php file.
But then you'll have another issue. Your autoloader isn't a real autoloader. Here's more info on that.