skip to Main Content

I am developing an application where I am getting messages from conversations. Here is my code:

// facebook SDK startup
$fb = new FacebookFacebook([
  'app_id' => '[my_app_id]',
  'app_secret' => '[my_app_secret]',
  'default_graph_version' => 'v2.4',
]);


$request = new FacebookFacebookRequest(
                          $session,
                        'GET',
                        '/{converation-id}',
                        array(
                            'fields' => 'gender'
                                )
                            );

                            $response = $request->execute();
                            $graphObject = $response->getGraphObject();
                            /* handle the result */
                          var_dump($graphObject);

When I run this, i get this error:

Undefined variable: $session

I know that I haven’t initialized it but I am confused that what value do I have to assign to it or where do I need to connect this variable? Any suggestions?

2

Answers


  1. This one is outdated.

    For the newest versions you don’t need the $session variable. In the newer versions the $request->execute() method is missing, too.

    Try something like this:

    // facebook SDK startup
    $fb = new FacebookFacebook([
      'app_id' => '[my_app_id]',
      'app_secret' => '[my_app_secret]',
      'default_graph_version' => 'v2.4',
    ]);
    
    $request = $fb->request('GET', '/{conversation-id}', array('fields' => 'gender'));
    
    // Send the request to Graph
    try {
      $response = $fb->getClient()->sendRequest($request);
    } catch(FacebookExceptionsFacebookResponseException $e) {
      // When Graph returns an error
      echo 'Graph returned an error: ' . $e->getMessage();
      exit;
    } catch(FacebookExceptionsFacebookSDKException $e) {
      // When validation fails or other local issues
      echo 'Facebook SDK returned an error: ' . $e->getMessage();
      exit;
    }
    
    $graphNode = $response->getGraphNode();
    var_dump($graphNode);
    
    Login or Signup to reply.
  2. I got the same problem from FB SDK.
    I have tried the code from Fabian, and it requests me to add the access token now.

    $request = $fb->request(
      'GET',
      '/{conversation-id}',
      array(
        'fields' => 'posts.limit(10)',
        'access_token' => '{token_id}'
      )
    );
    

    And it works! I got the array result.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search