Viewing 4 replies - 1 through 4 (of 4 total)
  • I extended this plugin so I could do a search which wasn’t restricted to a specific username.

    in twitter-feed-for-developers.php I put this in

    /* implement searchTweets */
    function searchTweets($count = 20, $options = false) {
    
      $config['key'] = get_option('tdf_consumer_key');
      $config['secret'] = get_option('tdf_consumer_secret');
      $config['token'] = get_option('tdf_access_token');
      $config['token_secret'] = get_option('tdf_access_token_secret');
      $config['screenname'] = get_option('tdf_user_timeline');
      $config['cache_expire'] = intval(get_option('tdf_cache_expire'));
      if ($config['cache_expire'] < 1) $config['cache_expire'] = 3600;
      $config['directory'] = plugin_dir_path(__FILE__);
    
      $obj = new StormTwitter($config);
      $res = $obj->searchTweets($count, $options);
      update_option('tdf_last_error',$obj->st_last_error);
      return $res;
    
    }

    and in StormTwitter.class.php I add these 2

    function searchTweets($count = 20,$options = false){
        if ($count > 20) $count = 20;
        if ($count < 1) $count = 1;
    
        $default_options = array('trim_user'=>true, 'exclude_replies'=>true, 'include_rts'=>false);
    
        if ($options === false || !is_array($options)) {
          $options = $default_options;
        } else {
          $options = array_merge($default_options, $options);
        }
    
        $result = $this->checkValidCache("SEARCH",$options);
    
        error_log($result);
        if ($result !== false) {
          return $this->cropTweets($result,$count);
        }
        //If we're here, we need to load.
        $result = $this->oauthSearchTweets($options);
        if (isset($result['errors'])) {
          if (is_array($results) && isset($result['errors'][0]) && isset($result['errors'][0]['message'])) {
            $last_error = $result['errors'][0]['message'];
          } else {
            $last_error = $result['errors'];
          }
          return array('error'=>'Twitter said: '.$last_error);
        } else {
          return $this->cropTweets($result,$count);
        }
      }
      private function oauthSearchTweets($options){
        $key = $this->defaults['key'];
        $secret = $this->defaults['secret'];
        $token = $this->defaults['token'];
        $token_secret = $this->defaults['token_secret'];
        $cachename = "SEARCH-".$this->getOptionsHash($options);
        $options = array_merge($options, array('count' => 20));
        if (empty($key)) return array('error'=>'Missing Consumer Key - Check Settings');
        if (empty($secret)) return array('error'=>'Missing Consumer Secret - Check Settings');
        if (empty($token)) return array('error'=>'Missing Access Token - Check Settings');
        if (empty($token_secret)) return array('error'=>'Missing Access Token Secret - Check Settings');
    
        $connection = new TwitterOAuth($key, $secret, $token, $token_secret);
        $result = $connection->get('search/tweets', $options);
    
        if (is_file($this->getCacheLocation())) {
          $cache = json_decode(file_get_contents($this->getCacheLocation()),true);
        }
    
        if (!isset($result['errors'])) {
          $cache[$cachename]['time'] = time();
          $cache[$cachename]['tweets'] = $result;
          $file = $this->getCacheLocation();
          file_put_contents($file,json_encode($cache));
        } else {
          if (is_array($results) && isset($result['errors'][0]) && isset($result['errors'][0]['message'])) {
            $last_error = '['.date('r').'] Twitter error: '.$result['errors'][0]['message'];
            $this->st_last_error = $last_error;
          } else {
            $last_error = '['.date('r').'] Twitter returned an invalid response. It is probably down.';
            $this->st_last_error = $last_error;
          }
        }
    
        return $result;
      }

    and then I used it like this:

    $test = array("q"=>"%23Cambridge");
    	$res =  searchTweets(20, $test);

    I would like to submit it as a patch but if it works for you tell me and I will do so

    Thread Starter HarroH

    (@harroh)

    Hi hendecasyllabic,

    Very nice addition. Worked well for me.

    Two points of feedback however:

    searchTweets and getTweets in twitter-feed-for-developers.php are almost identical. So that can be written more efficient I guess.

    And remove the line:
    error_log($result);
    when the results are fine it returns an array, which will make this function give an error on E_ALL as error_log can only process strings.

    But again, nice work!

    Plugin Author Liam Gladdy

    (@lgladdy)

    Hi hendecasyllabic,

    Feel free to edit the code on github and add this then merge request to us and we’ll include this in the next release 🙂

    https://github.com/stormuk/storm-twitter

    Thanks!

    I am trying to integrate your changes into the current plugin and I am not getting any results (nor errors). I have been using the example layout code as a starting point (https://github.com/stormuk/storm-twitter-for-wordpress/wiki/Example-code-to-layout-tweets) and I have tried changing the initial call from:

    $tweets = getTweets(4, ‘twitteruser’, array(‘include_rts’=>true));
    foreach($tweets as $tweet){
    if($tweet[‘text’]){
    echo ‘<li class=”tweet”>’;
    …etc.

    to:

    $tweets = searchTweets(4, array(‘q’=>’%23Cambridge’));
    foreach($tweets as $tweet){
    if($tweet[‘text’]){
    echo ‘<li class=”tweet”>’;
    …etc.

    But no results are being displayed. Is there something that I am doing wrong or missing? I have implemented the changes to both twitter-feed-for-developers.php and StormTwitter.class.php and as I said I am not seeing any errors.

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Search Feed’ is closed to new replies.