Facebook PHP SDK v5 : Read Page Public Feeds?

老子叫甜甜 提交于 2019-12-01 13:10:33

问题


(I'm new to Facebook SDK)

I installed Facebook PHP SDK v5 by this way:

# ./composer.phar require facebook/php-sdk-v4

Then here is my first page which gets the User Access Token successfully.

<?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 );
?>

(There is another page called: login-callback.php which also works fine; to get the User Access Token saved into Session.)

Now ... as mentioned with comments in above file, the whole thing works util this comment session:

// -----------------------------------------------------------------    
// --------------------- THIS WORKS UP TO HERE! --------------------
// -----------------------------------------------------------------

It shows something like this:

Logged in as: Robert Walters (48650862XXXXXXX)

Problem

But after the above certain point (as we see), i start reading the public feeds from /Google/feed?limit=5 page.

Then the error comes:

PHP Fatal error:  Class 'Facebook\\FacebookSession' not found in ...

...............

Questions

  1. Strangely, i didn't find FacebookSession.php file in my SDK.
  2. After that, how do i properly read the public feeds from Users & Pages?

Thanks all.

来源:https://stackoverflow.com/questions/37599438/facebook-php-sdk-v5-read-page-public-feeds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!