You are here

class TwitterBlockSearch in Twitter Block 6

Same name and namespace in other branches
  1. 7 twitter_block.class.php \TwitterBlockSearch

TwitterBlockSearch provides the class for using the Twitter Search API.

For more information on the API, see http://dev.twitter.com/doc/get/search

Hierarchy

Expanded class hierarchy of TwitterBlockSearch

File

./twitter_block.class.php, line 20
Lightweight implementation of the Twitter API in PHP.

View source
class TwitterBlockSearch {

  // HTTP status code returned
  private $http_status;

  // Search parameters as defined by the API to be used w/ http_build_query
  private $options = array();

  // Determines which getter to use.
  private $search_type;

  // What were we looking for again?
  private $search_string;
  private $twitter_name;
  private $include_rts;

  // The API type we'll pull data from.
  private $api;

  // The url including the encoded query
  public $url_query;
  public function __construct($config = array()) {

    // @todo: Make this a configurable URL.
    $this->url_query = 'http://search.twitter.com/search.json?';
    $this->search_type = $config['search_type'];
    $this->api = in_array($this->search_type, array(
      'getTweetsFrom',
    )) ? 'rest' : 'search';
    if ($config['search_type'] == 'searchHashtag') {

      // We presume the search string is already validated.
      $this->search_string = $config['search_string'];
    }
    else {
      $this->twitter_name = $config['search_string'];
    }
    $this->include_rts = $config['include_rts'];
    $count = $this->api == 'rest' ? 'count' : 'rpp';

    // The number of tweets to return per page.
    if (!empty($config['results_per_page'])) {
      $this->options[$count] = $config['results_per_page'];
    }
    else {
      $this->options[$count] = variable_get('twitter_block_default_results_per_page', 15);
    }

    // Filter by language, but only if there is one set in the config.
    if (isset($config['lang']) && !empty($config['lang'])) {
      $this->options['lang'] = $config['lang'];
    }
  }

  /**
   * Retrieve JSON encoded search results
   */
  public function getJSON() {
    $response = call_user_func(array(
      $this,
      $this->search_type,
    ));
    $return = array(
      'status' => TRUE,
      'results' => array(),
      'debug' => $response,
    );
    if (empty($response)) {
      $return['status'] = 'Empty response from Twitter.';
    }
    else {
      $decoded = json_decode($response);
      if (empty($decoded)) {
        $return['status'] = 'Response from Twitter is not valid JSON data.';
      }
      elseif ($this->api == 'rest') {
        $data = $decoded;
      }
      else {
        $data = $decoded->results;
      }

      // An error from the API.
      if (!empty($data->error)) {
        $return['status'] = $data->error;
      }
      else {
        $return['results'] = $data;
      }
    }
    return $return;
  }

  /**
   * Returns the most recent tweets from $twittername
   * @param string $twittername to search. Note: begins with @
   * @return string $json JSON encoded search response
   */
  private function getTweetsFrom() {
    $this->options['screen_name'] = $this->twitter_name;
    $this->options['include_rts'] = $this->include_rts;

    // @todo: Make this URL a configurable option.
    $this->url_query = 'http://api.twitter.com/1/statuses/user_timeline.json?';
    $json = $this
      ->search();
    return $json;
  }

  /**
   * Returns the most recent mentions (status containing @twittername)
   * @param string $twittername to search. Note: begins with @
   * @return string $json JSON encoded search response
   */
  private function getMentions() {
    $this->options['q'] = "@{$this->twitter_name}";
    $json = $this
      ->search();
    return $json;
  }

  /**
   * Returns the most recent @replies to $twittername.
   * @param string $twittername to search. Note: begins with @.
   * @return string JSON encoded search response
   */
  private function getReplies() {
    $this->options['q'] = "to:{$this->twitter_name}";
    $json = $this
      ->search();
    return $json;
  }

  /**
   * Returns the most recent tweets containing a string or hashtag.
   * @param string $hashtag to search. May or may not begin with #.
   * @return string JSON encoded search response
   */
  private function searchHashtag() {
    $this->options['q'] = $this->search_string;
    $json = $this
      ->search();
    return $json;
  }

  /**
   * Returns the last HTTP status code
   * @return integer
   */
  public function lastStatusCode() {
    return $this->http_status;
  }

  /**
   * Executes a Twitter Search API call
   * @return string JSON encoded search response.
   */
  function search() {

    // Build a URL query to send to Twitter
    $this->url_query .= drupal_http_build_query($this->options);
    if (variable_get('twitter_block_debug_mode', FALSE)) {
      watchdog('Twitter Block', 'DEBUG: URL: twitter_block_url', array(
        'twitter_block_url' => $this->url_query,
      ), WATCHDOG_NOTICE);
    }

    // Query Twitter
    $twitter_data = drupal_http_request($this->url_query);

    // Set the response status code, or the error code if an error occurred
    $this->http_status = $twitter_data->code;
    if (isset($twitter_data->error)) {
      if (variable_get('twitter_block_debug_mode', FALSE)) {
        watchdog('Twitter Block', 'DEBUG: Returned data: returned_tweet_data', array(
          'returned_tweet_data' => print_r($twitter_data->error, TRUE),
        ), WATCHDOG_NOTICE);
      }
      return;
    }
    return $twitter_data->data;
  }
  function getApi() {
    return $this->api;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TwitterBlockSearch::$api private property
TwitterBlockSearch::$http_status private property
TwitterBlockSearch::$include_rts private property
TwitterBlockSearch::$options private property
TwitterBlockSearch::$search_string private property
TwitterBlockSearch::$search_type private property
TwitterBlockSearch::$twitter_name private property
TwitterBlockSearch::$url_query public property
TwitterBlockSearch::getApi function
TwitterBlockSearch::getJSON public function Retrieve JSON encoded search results
TwitterBlockSearch::getMentions private function Returns the most recent mentions (status containing @twittername)
TwitterBlockSearch::getReplies private function Returns the most recent @replies to $twittername.
TwitterBlockSearch::getTweetsFrom private function Returns the most recent tweets from $twittername
TwitterBlockSearch::lastStatusCode public function Returns the last HTTP status code
TwitterBlockSearch::search function Executes a Twitter Search API call
TwitterBlockSearch::searchHashtag private function Returns the most recent tweets containing a string or hashtag.
TwitterBlockSearch::__construct public function