PHP SDK Demystified – How PHP SDK calls the Graph API (Part 2)

In the last article, we have discussed how the PHP SDK invokes the _graph method and handle the parameters passed.  Now, let’s take a look at how the PHP SDK actually send out the Graph API request to Facebook,

    $result = json_decode($this->_oauthRequest(
      $this->getUrl('graph', $path),
    ), true);

    // results are returned, errors are thrown
    if (is_array($result) && isset($result['error'])) {
      $e = new FacebookApiException($result);
      switch ($e->getType()) {
        // OAuth 2.0 Draft 00 style
        case 'OAuthException':
        // OAuth 2.0 Draft 10 style
        case 'invalid_token':
      throw $e;
    return $result;

The _oauthRequest protected method inside the PHP SDK. will first be invoked   It is responsible for formatting the OAuth Request that is going to send to Faceboook.  The response from Facebook is the decoded (note: all responses for the Graph API from Facebook are JSON objects).

To make our lives easier in handling error returned from Faecbook (e.g. the object cannot be found, invalid request etc), the PHP SDK will then check to see if the result returned from Facebook is actually an error.  If it is, then the PHP SDK will create and throw out a FacebookApiException.

Note: If you have missed the article on the FacebookApiException class, it is here.

This entry was posted in PHP SDK Demystified and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *