Class 'FacebookSession' not found - API Facebook 4.0 - php

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.

Related

Getting all members list (from a group I own) using Facebook Graph API - doesn't get the whole list - What part is wrong in my code?

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

PHP Facebook SDK Posting to wall with SDK 4

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';
}

I can't get any value from php Facebook graph api

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.

Getting A Users Data With Facebook API

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

PHP Facebook SDK 4.0 post as a page server to server

I'm trying to post as a page on a Facebook page since 6 days without success, which is really frustrating using new facebook SDK 4.0.4.
I'm under PHP 5.4 (as requested by Facebook 4.0 SDK version).
I already read some times all the facebook documentation (which isn't so clear) https://developers.facebook.com/docs/reference/php/4.0.0.
For logging-in:
<?php
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookSession.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRedirectLoginHelper.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRequest.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookResponse.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookSDKException.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRequestException.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookOtherException.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookAuthorizationException.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/GraphObject.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/GraphSessionInfo.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookServerException.php' );
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;
use Facebook\FacebookServerException;
session_start();
// init app with app id and secret
FacebookSession::setDefaultApplication($site["facebook_appid"],$site["facebook_appsecret"] );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper($site["url"].$_SERVER['REQUEST_URI']);
// 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;
}
echo 'Logout';
} 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;
}
echo 'Login';
}
///////////////////////////
// SHORT LIVED TOKEN
$request = new FacebookRequest( $session, "GET", "/".$site["facebook_appid"]."/accounts?access_token=".$site["facebook_appid"]."|".$site["facebook_appsecret"]."&grant_type=client_credentials" );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject()->asArray();
if (DebugMode) e($graphObject);
$site['facebook_short-lived_access_token'] = $graphObject['data'][0]->access_token;
////////////////////////
///////////////////////////
// LONG LIVED TOKEN
$request = new FacebookRequest( $session, "GET", "/oauth/access_token?grant_type=fb_exchange_token&client_id=".$site["facebook_appid"]."&client_secret=".$site["facebook_appsecret"]."&fb_exchange_token=" .$site['facebook_short-lived_access_token']);
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject()->asArray();
if (DebugMode) e($graphObject);
$site['facebook_long-lived_access_token'] = $graphObject['access_token'];
$_SESSION['fb_token'] = $site['facebook_long-lived_access_token'];
if (DebugMode) e($site['facebook_long-lived_access_token']);
?>
And for publishing:
<?php
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRequest.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/GraphObject.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookSDKException.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookRequestException.php' );
require_once( BaseRoot. '/include/scripts/facebook_php_sdk/src/Facebook/FacebookPermissionException.php' );
use Facebook\FacebookRequest;
use Facebook\GraphObject;
use Facebook\FacebookRequestException;
use Facebook\FacebookSDKException;
use Facebook\FacebookPermissionException;
if($session) {
try {
$response = (new FacebookRequest(
$session, 'POST', "/".$site["facebook_page_id"]."/feed",
array(
'access_token' => $_SESSION['fb_token'],
'link' => $site["url"].'/url',
'name' => 'link name',
'caption' => 'link caption',
'description' => 'link description',
'picture' => $site["url"].'/alpha.png',
'message' => 'User provided message'
)
))->execute()->getGraphObject();
echo "Posted with id: " . $response->getProperty('id');
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
?>
I first get a session ID, then request and obtain a short-lived token, with which i request and obtain a long-lived token (60 days validity). Then I continue with the publish script using the last obtained token.
I submit a post and it return back a post-id like it works, but nothing on the page, nothing in my personal Facebook history.
My application is in development mode and was not reviewed by Facebook, but it seems to not be necessary anymore: http://www.nextscripts.com/known-issues/facebook-issues/
Any idea where I'm wrong?
I'm not realy into the facebook SDK, but I noticed one line that I suspect to be incorrect:
echo 'Login';
The facebook documentation about this function can be found here.
It states that you schould, to ask for extra permissions, give an array with the index 'scope' and as value in string with a comma between every permission you want to request instead of an array with indexes called to permission but without any data in it. I suspect it should be something like:
$helper->getLoginUrl( array( 'scope' => 'public_profile,email,user_friens,publish_stream,<and so on>'));
I hope this helps you.

Categories