适用于PHP的Facebook SDK v4最小示例 [英] Facebook SDK v4 for PHP Minimal Example

查看:105
本文介绍了适用于PHP的Facebook SDK v4最小示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获得最小的例子

I'm trying to get the minimal example

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
}

自述文件中运行,但我不理解第一行代码是什么意思.我必须在哪里将使用该最小代码示例的PHP文件放入SDK文件结构中.我直接在src文件夹中尝试过,但是返回了以下PHP错误

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

文件结构如下

├── 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

推荐答案

最近解决了此问题. 由于sdk有可用的autoload.php文件,因此您无需使用require等. 只需在开始时包含该autoload.php

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

?>

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

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

如果您将目录的名称更改为将所有文件从/src/Facebook/更改为/sdk/,则此行是默认代码,则只需替换名称 始终使用die(__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.

这篇关于适用于PHP的Facebook SDK v4最小示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆