You are here

class InstagramRequest in Instagram Block 7

@file Instagram classes to integrate with the Instagram API.

Hierarchy

Expanded class hierarchy of InstagramRequest

File

./instagram_block.lib.php, line 8
Instagram classes to integrate with the Instagram API.

View source
class InstagramRequest {

  /**
   * @var array $response.
   */
  protected $response;

  /**
   * @var array $config
   */
  protected $config;

  /**
   * @var array $values
   */
  protected $values;

  /**
   * Constructs the request object.
   *
   * @param array $config
   * @param array $values
   */
  public function __construct(array $config, array $values) {
    $this->config = $config;
    $this->values = $values;
  }

  /**
   * Builds a request for {user} media.
   */
  public function requestUserMedia() {
    $url = 'https://api.instagram.com/v1/users/self/media/recent/';
    $params = array(
      'access_token' => $this->config['access_token'],
      'count' => $this->values['count'],
    );
    $this->response = $this
      ->request($url, $params, 'GET');
  }

  /**
   * Builds a request for {tag} media
   */
  public function requestTagMedia() {
    $url = "https://api.instagram.com/v1/tags/" . $this->values['tag'] . "/media/recent/";
    $params = array(
      'access_token' => $this->config['access_token'],
      'count' => $this->values['count'],
    );
    $this->response = $this
      ->request($url, $params, 'GET');
  }

  /**
   * Performs a request.
   *
   * @param string $url
   * @param array $params
   * @param string $method
   *
   * @throws \Exception
   */
  protected function request($url, $params = array(), $method = 'GET') {
    $data = '';
    if (count($params) > 0) {
      if ($method == 'GET') {
        $url .= '?' . http_build_query($params, '', '&');
      }
      else {
        $data = http_build_query($params, '', '&');
      }
    }
    $headers = array();
    $headers['Authorization'] = 'Oauth';
    $headers['Content-type'] = 'application/x-www-form-urlencoded';
    $response = $this
      ->doRequest($url, $headers, $method, $data);
    if (!isset($response->error)) {
      return $response->data;
    }
    else {
      $error = $response->error;
      if (!empty($response->data)) {
        $data = $this
          ->parse_response($response->data);
        if (isset($data->error)) {
          $error = $data->error;
        }
        elseif (isset($data->meta)) {
          $error = new Exception($data->meta->error_type . ': ' . $data->meta->error_message, $data->meta->code);
        }
      }
      watchdog('instagram_block', $error);
    }
  }

  /**
   * Actually performs a request.
   *
   * This method can be easily overriden through inheritance.
   *
   * @param string $url
   *   The url of the endpoint.
   * @param array $headers
   *   Array of headers.
   * @param string $method
   *   The HTTP method to use (normally POST or GET).
   * @param array $data
   *   An array of parameters
   *
   * @return
   *   stdClass response object.
   */
  protected function doRequest($url, $headers, $method, $data) {
    return drupal_http_request($url, array(
      'headers' => $headers,
      'method' => $method,
      'data' => $data,
    ));
  }

  /**
   * Parses the response.
   */
  protected function parse_response($response) {

    // http://drupal.org/node/985544 - json_decode large integer issue
    $length = strlen(PHP_INT_MAX);
    $response = preg_replace('/"(id|in_reply_to_status_id)":(\\d{' . $length . ',})/', '"\\1":"\\2"', $response);
    return json_decode($response);
  }

  /**
   * Get an array of InstagramPosts objects.
   */
  public function get_instagram_posts() {
    $response = $this
      ->parse_response($this->response);

    // Check on successfull call
    if ($response) {
      $posts = array();
      foreach ($response->data as $post) {
        $posts[] = new InstagramPost($post);
      }
      return $posts;
    }
    else {
      return array();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
InstagramRequest::$config protected property
InstagramRequest::$response protected property
InstagramRequest::$values protected property
InstagramRequest::doRequest protected function Actually performs a request.
InstagramRequest::get_instagram_posts public function Get an array of InstagramPosts objects.
InstagramRequest::parse_response protected function Parses the response.
InstagramRequest::request protected function Performs a request.
InstagramRequest::requestTagMedia public function Builds a request for {tag} media
InstagramRequest::requestUserMedia public function Builds a request for {user} media.
InstagramRequest::__construct public function Constructs the request object.