PHP SDK Demystified – How PHP SDK calls the Graph API

In the last article, we have looked at the api() method in the PHP SDK.  To recap, the api() method is a pubic method that we used to call Graph API and Old REST API.

The api() method is defined as:

  public function api(/* polymorphic */) {
    $args = func_get_args();
    if (is_array($args[0])) {
      return $this->_restserver($args[0]);
    } else {
      return call_user_func_array(array($this, '_graph'), $args);
    }
  }

In this article, we will take a closer look at how it calls the Graph API.

If the first parameter (i.e. $args[0]) passed is not an array, the function _graph will be called. The _graph is a protected function (so our scripts cannot call it directly) and is defined as:

  /**
   * Invoke the Graph API.
   *
   * @param String $path the path (required)
   * @param String $method the http method (default 'GET')
   * @param Array $params the query/post data
   * @return the decoded response object
   * @throws FacebookApiException
   */
  protected function _graph($path, $method='GET', $params=array()) {
    if (is_array($method) && empty($params)) {
      $params = $method;
      $method = 'GET';
    }
    $params['method'] = $method; // method override as we always do a POST

    $result = json_decode($this->_oauthRequest(
      $this->getUrl('graph', $path),
      $params
    ), 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':
          $this->setSession(null);
      }
      throw $e;
    }
    return $result;
  }

As we can see, _graph takes 3 parameter:

  • $path
    This is the path for the Graph object that the Graph API request is working on, e.g. “/me”
  • $method
    This is the action that the request is doing.  It can be “GET”, “POST”, or “DELETE”.
  • $array
    This is the parameters that we want to passed along with the Graph API request.  Fore example, it can be an array object corresponding to a “post” Graph object  that we want published to a user’s wall.

In many cases, while we issue “GET” graph API requests, we do not specify the $method parameter.  OK, we can see that the default value for the  $method parameter is “GET”.

Now, let’s recall how you call the api() method for “GET” graph API requests with parameters …. isn’t the parameters are passed as the 2nd parameter in the api() method instead of the 3rd?  That’s strange, right?  But if we take a look at the first few lines of the _graph method, this can be explained.  As seen, there are codes:

    if (is_array($method) && empty($params)) {
      $params = $method;
      $method = 'GET';
    }

So, if we passed an array as the 2nd parameter when calling the api() method, the PHP SDK will take it as the $params and treat our request as a “GET” request.

To me, this is an interesting handling.  If I were the developer of the PHP SDK, I will probably define the 2nd parameter as $param and the 3rd parameter as $method.

The next line of code in the _graph method is also interesting, it takes the $method value and set it as the ‘method’ element in the array.

… this is pretty long already and I’d better stop here.  We will continue to look at how the PHP SDK calls the Graph API in the next article.

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

One Response to PHP SDK Demystified – How PHP SDK calls the Graph API

  1. Pingback: PHP SDK Demystified – How PHP SDK calls the Old REST API | Wing's Blog on Facebook Development & Virtualization

Leave a Reply

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