【问题标题】:Is it possible Facebook PHP SDK 4.0 and Graph API v2.8?是否可以使用 Facebook PHP SDK 4.0 和 Graph API v2.8?
【发布时间】:2017-03-05 17:50:28
【问题描述】:

我正在使用 Facebook PHP SDK 4.0 和 Graph API v2.2。我想知道这个 PHP SDK 4.0 是否可以使用 Graph API v2.8(我的服务器上安装了 PHP 5.2,但我不能使用 PHP SDK 5.0)。

目前,我使用以下代码获取用户ID

$facebook = new Facebook(array(
  'appId'  => $facebookAppId,
  'secret' => $facebookSecret
));
$user = $facebook->getUser();
if ($user) {
    $user_profile = $facebook->api('/me');
    $user_id = $user_profile['id'];
}

我被警告说 API v2.2 将在本月下旬被弃用,我正在尝试将我的代码迁移到 API v2.8。

非常感谢。

【问题讨论】:

  • IIRC SDK v4 没有提供通过配置/初始化时指定 API 版本的方法。如果直接修改源文件没有问题,覆盖 base_facebook.php 中的基本 API 地址可能是为所有调用指定 API 版本的最简单方法。
  • 嗨。非常感谢您的回答。我只需要让 Facebook 用户登录,并获取他们的用户 ID。对于该任务,我怀疑我什至不需要 SDK。我一直在浏览这个简单的提案 (wp-time.com/login-with-facebook-using-api-in-php),我正在考虑在没有 SDK 的情况下实现它。

标签: facebook-graph-api facebook-php-sdk php-5.2


【解决方案1】:

// 初始化Facebook PHPSDK5和v2.8。

$fb = new Facebook\Facebook([
  'app_id'                => 'xxxxxxxxxxxx',
  'app_secret'            => 'xxxxxxxxxxxxxxxxxxxxxxxx',
  'default_graph_version' => 'v2.8',
]);

$helper = $fb->getRedirectLoginHelper();
try {
  $accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  // There was an error communicating with Graph
  echo $e->getMessage();
  exit;
}

if (isset($accessToken)) {
  // Logged in.
  $_SESSION['facebook_access_token'] = (string) $accessTokenLong;
}

$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);

$me = $fb->get('/me');
$me_array = $me->getGraphEdge()->asArray();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    相关资源
    最近更新 更多