沃梦达 / 编程问答 / php问题 / 正文

Facebook SDK v4 for PHP 最小示例

Facebook SDK v4 for PHP Minimal Example(Facebook SDK v4 for PHP 最小示例)

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

问题描述

我正在尝试获取最小的示例

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
}

来自 README 工作,但我不明白第一行代码是什么意思.我必须在 SDK 文件结构中使用该最小代码示例将 PHP 文件放在哪里.我直接在 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 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>';
}

?>

在此之后,您必须检查 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.

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

本文标题为:Facebook SDK v4 for PHP 最小示例

基础教程推荐