Attila Naghi
Attila Naghi

Reputation: 2686

Getting the latest wall post from my profile using facebook sdk 5 in PHP

This is my PHP CODE. After the login process I want to display the latest wall posts.

$fb = new Facebook\Facebook([
  'app_id' => 'xxxxxxxx',
  'app_secret' => 'xxxxxxxxxxx',
  'default_graph_version' => 'v2.4',
  'default_access_token' => isset($_SESSION['facebook_access_token']) ? $_SESSION['facebook_access_token'] : 'xxxxxxxxxxxxxx|xxxxxxxxxxxxxxxxxxxxx'
]);


try {
    // $response = $fb->get('/me?fields=id,name,email');
    // $user = $response->getGraphUser();
    // echo 'Name: ' . $user['name'];
    // echo 'Email: ' . $user['email'];
    // echo 'ID: ' . $user['id'];
    $data = ( new Facebook\FacebookRequest( $_SESSION['facebook_access_token'], 'GET', '/me/posts') )->execute()->getGraphObject()->getPropertyAsArray("data");

    foreach ($data as $post){
        $postId = $post->getProperty('id');
        $postMessage = $post->getProperty('message');
        print "$postId - $postMessage <br />";
    }
    exit; //redirect, or do whatever you want
} catch(Facebook\Exceptions\FacebookResponseException $e) {
  //echo 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
  //echo 'Facebook SDK returned an error: ' . $e->getMessage();
}

$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes', 'user_about_me'];
$loginUrl = $helper->getLoginUrl('http://attila-naghi.com/facebook/login.php', $permissions);
echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';

But I got this error message:

Catchable fatal error: Argument 1 passed to 
Facebook\FacebookRequest::__construct() must be an instance of 
Facebook\FacebookApp, string given, called in....

in line 97. This is the content from line 97 from my class:

 public function __construct(FacebookApp $app = null, $accessToken = null, $method = null, $endpoint = null, array $params = [], $eTag = null, $graphVersion = null)
    {
        $this->setApp($app);
        $this->setAccessToken($accessToken);
        $this->setMethod($method);
        $this->setEndpoint($endpoint);
        $this->setParams($params);
        $this->setETag($eTag);
        $this->graphVersion = $graphVersion ?: Facebook::DEFAULT_GRAPH_VERSION;
    }

My quess that somehow I am not passing my token from my session in the right way to the constructor. but I don't understand why ? Can someone explain me how doest it work. The documentation it is not the best one from facebook sdk. Thx in advance.

Upvotes: 0

Views: 161

Answers (1)

Jan
Jan

Reputation: 43169

The first argument needs to be a FacebookApp variable, not the token. So change the line to:

$data = ( new Facebook\FacebookRequest( $fb, $_SESSION['facebook_access_token'], 'GET', '/me/posts') )->execute()->getGraphObject()->getPropertyAsArray("data");

Upvotes: 1

Related Questions