(我是Facebook SDK的新手)
我通过这种方式安装了Facebook PHP SDK v5:
# ./composer.phar require facebook/php-sdk-v4
然后这是我的第一页成功获得User Access Token
。
<?php
require_once 'vendor/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '<my-valid-app-id>',
'app_secret' => '<my-valid-app-secret>',
'default_graph_version' => 'v2.6',
'persistent_data_handler'=>'session',
]);
$helper = $fb->getRedirectLoginHelper();
$accessToken = "";
if ( isset($_SESSION["facebook_access_token"]) ) {
$accessToken = $_SESSION["facebook_access_token"];
}
if ( $accessToken=="" ) {
$loginUrl = $helper->getLoginUrl('http://www.example.com/login-callback.php', []);
echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';
exit;
}
try {
$response = $fb->get('/me', $accessToken);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$me = $response->getGraphUser();
echo 'Logged in as: ' . $me->getName() . ' (' . $me->getId() . ')';
echo '<hr>';
// -----------------------------------------------------------------
// --------------------- THIS WORKS UP TO HERE! --------------------
// -----------------------------------------------------------------
/**
* GET PUBLIC FEEDS
*/
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
$session = new FacebookSession( $accessToken );
$request = new FacebookRequest(
$session,
'GET',
'/Google/feed?limit=5'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
print_r( $graphObject );
?>
(还有另一个名为login-callback.php
的网页也可以正常工作;将User Access Token
保存到会话中。)
现在......正如上面文件中的评论所提到的那样,整个过程都有效利用这个评论会话:
// -----------------------------------------------------------------
// --------------------- THIS WORKS UP TO HERE! --------------------
// -----------------------------------------------------------------
它显示如下:
Logged in as: Robert Walters (48650862XXXXXXX)
但经过上述某点(如我们所见),我开始从/Google/feed?limit=5
页面阅读公开信息。
然后出现错误:
PHP Fatal error: Class 'Facebook\\FacebookSession' not found in ...
...............
FacebookSession.php
文件。谢谢大家。