Facebook SDK v4 for PHP Minimal Example

Nic*_*ser 11 php namespaces

我试图得到最小的例子

using Facebook\FacebookSession;

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
}
Run Code Online (Sandbox Code Playgroud)

README工作,但我不明白第一行代码意味着什么.我在哪里必须使用SDK文件结构中的最小代码示例来放置PHP文件.我直接尝试在src文件夹中,但返回以下PHP错误

[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
Run Code Online (Sandbox Code Playgroud)

文件结构如下所示

??? 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
Run Code Online (Sandbox Code Playgroud)

小智 22

最近解决了这个问题.因为有sdk可用的autoload.php文件,你不需要使用require等,只需在开始时包含autoload.php

 <?php
session_start();
// added in v4.0.0
require_once 'autoload.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\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;

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

?>
Run Code Online (Sandbox Code Playgroud)

在此之后,您必须检查autoload.php文件中的路径

$base_dir = defined('FACEBOOK_SDK_V4_SRC_DIR') ? FACEBOOK_SDK_V4_SRC_DIR : __DIR__ . '/src/Facebook/';
Run Code Online (Sandbox Code Playgroud)

这行是默认代码,如果您更改了目录的名称,例如将所有文件从/ src/Facebook /放到/ sdk /然后只需替换名称,请始终检查包含的路径,die(__DIR__ . '/src/Facebook/'); 以确保它是否正确.

  • 您可能想要从答案中删除您的应用秘密 (10认同)

Ahm*_*met 10

这里有一个完整的(工作)示例:http://metah.ch/blog/2014/05/facebook-sdk-4-0-0-for-php-a-working-sample-to-get-started/

码:

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;

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

// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://www.metah.ch/' );

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 '<a href="' . $helper->getLoginUrl() . '">Login</a>';
}
Run Code Online (Sandbox Code Playgroud)


mam*_*dan 6

我想你需要改变这一行:

using Facebook\FacebookSession;
Run Code Online (Sandbox Code Playgroud)

use Facebook\FacebookSession;
Run Code Online (Sandbox Code Playgroud)

usephp中使用命名空间关键字更多是使用命名空间的php

更新:

使用命名空间不会自动包含脚本,您需要在自动加载中包含路径(如果有的话)或者只是:

include 'path/to/FacebookSession.php';
Run Code Online (Sandbox Code Playgroud)

Update2:首先,请阅读手册中的php命名空间,第二,不,如果它已经包含在原始类中,那么你没有.或者如果它符合psr-*那么你可以使用一些自动加载器或使用spl_autoload_register

  • 感谢您迄今为止提供的所有宝贵帮助.我仍然没有得到Github工作的最小例子.我希望v4 SDK有一个完整的教程. (2认同)