viktorruskai/facebook-graph-sdk

PHP Facebook SDK

6.0.2 2022-07-13 13:34 UTC

README

PHP PHPUnit PHPStan Latest Stable Version

此存储库是从原始包分叉的。它已被重构以处理PHP 8.0和PHP 8.1。

安装

可以使用Composer安装Facebook PHP SDK。运行此命令

$ composer require viktorruskai/facebook-graph-sdk

用法

注意:此版本的Facebook SDK for PHP需要PHP 5.4或更高版本。

用户个人资料简单GET示例。

$fb = new \Facebook\Facebook([
  'app_id' => '{app-id}',
  'app_secret' => '{app-secret}',
  'default_graph_version' => 'v2.10',
  //'default_access_token' => '{access-token}', // optional
]);

// Use one of the helper classes to get a Facebook\Authentication\AccessToken entity.
//   $helper = $fb->getRedirectLoginHelper();
//   $helper = $fb->getJavaScriptHelper();
//   $helper = $fb->getCanvasHelper();
//   $helper = $fb->getPageTabHelper();

try {
  // Get the \Facebook\GraphNodes\GraphUser object for the current user.
  // If you provided a 'default_access_token', the '{access-token}' is optional.
  $response = $fb->get('/me', '{access-token}');
} catch(\Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
  // When validation fails or other local issues
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

$me = $response->getGraphUser();
echo 'Logged in as ' . $me->getName();

完整文档、安装说明和示例可在此处找到。

测试

可以从根目录运行此命令来执行测试

注意:您可以在Facebook Developers上创建测试应用,然后从tests/FacebookTestCredentials.php.dist创建tests/FacebookTestCredentials.php并编辑它以添加您的凭据。

$ composer tests

默认情况下,测试将向Graph API发送实时HTTP请求。如果您没有网络连接,可以通过排除integration组来跳过这些测试。

$ composer tests-without-http-requests

贡献

为了使我们能够接受贡献,您首先必须签署贡献者许可协议。请参阅CONTRIBUTING以获取详细信息。

许可

请参阅许可文件以获取更多信息。