Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

namespaces - Facebook SDK v4 for PHP Minimal Example

I'm trying to get the minimal example

using FacebookFacebookSession;

FacebookSession::setDefaultApplication('YOUR_APP_ID','YOUR_APP_SECRET');

// Use one of the helper classes to get a FacebookSession object.
//   FacebookRedirectLoginHelper
//   FacebookCanvasLoginHelper
//   FacebookJavaScriptLoginHelper
// or create a FacebookSession with a valid access token:
$session = new FacebookSession('access-token-here');

// Get the GraphUser object for the current user:

try {
  $me = (new FacebookRequest(
    $session, 'GET', '/me'
  ))->execute()->getGraphObject(GraphUser::className());
  echo $me->getName();
} catch (FacebookRequestException $e) {
  // The Graph API returned an error
} catch (Exception $e) {
  // Some other error occurred
}

from the README working, but I don't understand what the first line of code means. Where do I have to put the PHP file using that minimal code example within the SDK file structure. I tried directly in the src folder, but that returns the following PHP error

[01-May-2014 20:12:26 Europe/Berlin] PHP Parse error:  syntax error, unexpected 'Facebook' (T_STRING) in /Applications/MAMP/htdocs/facebook-php-sdk-v4/src/test.php on line 9

The file structure looks like this

├── src
│?? ├── Facebook
│?? │?? ├── FacebookAuthorizationException.php
│?? │?? ├── FacebookCanvasLoginHelper.php
│?? │?? ├── FacebookClientException.php
│?? │?? ├── FacebookJavaScriptLoginHelper.php
│?? │?? ├── FacebookOtherException.php
│?? │?? ├── FacebookPermissionException.php
│?? │?? ├── FacebookRedirectLoginHelper.php
│?? │?? ├── FacebookRequest.php
│?? │?? ├── FacebookRequestException.php
│?? │?? ├── FacebookResponse.php
│?? │?? ├── FacebookSDKException.php
│?? │?? ├── FacebookServerException.php
│?? │?? ├── FacebookSession.php
│?? │?? ├── FacebookThrottleException.php
│?? │?? ├── GraphLocation.php
│?? │?? ├── GraphObject.php
│?? │?? ├── GraphSessionInfo.php
│?? │?? ├── GraphUser.php
│?? │?? └── fb_ca_chain_bundle.crt
│?? └── test.php
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

have recently solved this. as there is autoload.php file available with sdk you dont need to use require etc etc. just include that autoload.php on the start

 <?php
session_start();
// added in v4.0.0
require_once 'autoload.php';

use FacebookFacebookSession;
use FacebookFacebookRedirectLoginHelper;
use FacebookFacebookRequest;
use FacebookFacebookResponse;
use FacebookFacebookSDKException;
use FacebookFacebookRequestException;
use FacebookFacebookAuthorizationException;
use FacebookGraphObject;
use FacebookEntitiesAccessToken;
use FacebookHttpClientsFacebookCurlHttpClient;
use FacebookHttpClientsFacebookHttpable;

// start session

// init app with app id and secret
FacebookSession::setDefaultApplication( 'app-id','app-secret' );

// login helper with redirect_uri

    $helper = new FacebookRedirectLoginHelper('http://yourhost/facebook/' );

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 '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
} else {
  // show login url
  echo '<a href="' . $helper->getLoginUrl() . '">Login</a>';
}

?>

after this you must have to check the path in autoload.php file

$base_dir = defined('FACEBOOK_SDK_V4_SRC_DIR') ? FACEBOOK_SDK_V4_SRC_DIR : __DIR__ . '/src/Facebook/';

this line is default code if u have changed the name of directories like placed all the files from /src/Facebook/ to /sdk/ then just replace the name always check the included path by using die(__DIR__ . '/src/Facebook/'); to make sure if it is correct.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...