magdy-hakam / graph-sdk
Facebook SDK for PHP
5.9.1
2024-03-05 07:53 UTC
Requires
- php: ^8.0
Requires (Dev)
- guzzlehttp/guzzle: ~5.0
- mockery/mockery: ~0.8
- phpunit/phpunit: ~4.0
Suggests
- guzzlehttp/guzzle: Allows for implementation of the Guzzle HTTP client
- paragonie/random_compat: Provides a better CSPRNG option in PHP 5
README
此仓库包含开源PHP SDK,允许您从PHP应用程序中访问Facebook平台。
安装
Facebook PHP SDK可以使用Composer安装。运行此命令
composer require facebook/graph-sdk
请注意,当与Guzzle 6.x一起使用时,可能会出现问题。php-graph-sdk v5.x默认只与Guzzle 5.x兼容。然而,存在一种解决方案,可以使它与Guzzle 6.x兼容。
升级到v5.x
从v4.x升级?Facebook PHP SDK v5.x引入了破坏性更改。请在升级之前阅读升级指南。
用法
注意:此版本的Facebook SDK for PHP需要PHP 5.4或更高版本。
用户个人资料简单GET示例。
require_once __DIR__ . '/vendor/autoload.php'; // change path as needed $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();
完整的文档、安装说明和示例可在此处找到。
测试
- 运行测试需要Composer作为先决条件。全局安装composer,然后运行
composer install
安装所需的文件。 - 在Facebook开发者上创建测试应用程序,然后从
tests/FacebookTestCredentials.php.dist
创建tests/FacebookTestCredentials.php
,并将其编辑以添加您的凭据。 - 可以通过在根目录中运行以下命令来执行测试
$ ./vendor/bin/phpunit
默认情况下,测试将向Graph API发送实时HTTP请求。如果您没有互联网连接,可以跳过这些测试,通过排除integration
组。
$ ./vendor/bin/phpunit --exclude-group integration
贡献
为了接受您的贡献,您必须首先签署贡献者许可协议。有关详细信息,请参阅CONTRIBUTING。
许可证
有关更多信息,请参阅许可证文件。
安全漏洞
如果您发现了一个安全漏洞,请直接联系维护者me@sammyk.me。