Fetch Tweets of Search query data by Twitter API using PHP ( 2 Points)

Hemanth Pasagadugula
04-01-2017 02:35PM
3 months ago





For Fetching the search query Tweets data from Twitter API, you have to first create a New Twitter app from the URL below:
  • After Creating the App, you need to use the below credentials:
    1) CONSUMER_KEY
    2) CONSUMER_SECRET
    3) ACCESS_TOKEN
    4) ACCESS_TOKEN_SECRET

    from the App you created in the code below. You need to download the TwitterOauth Library from the link and please use the same path while including in your project:


    <?php
    require "abraham/twitteroauth/autoload.php";
    require_once 'abraham/twitteroauth/TwitterOAuth.php';
     
    define('CONSUMER_KEY', 'xxxx');
    define('CONSUMER_SECRET', 'xxxx');
    define('ACCESS_TOKEN', 'xxxx');
    define('ACCESS_TOKEN_SECRET', 'xxxx');
     
    function search(array $query)
    {
      $toa = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
      return $toa->get('search/tweets', $query);
    }
     
    
    $max_id = "";
    foreach (range(1, 2) as $i) {
      $query = array(
        "q" => "happy",
    "count" => 100
      );
     
      $results = search($query);
      foreach ($results->statuses as $result) {
    var_dump($result);
    }
    }


  • You an fetch the data you want as shown below:

    echo $result->user->name;  // Gets the Name of who Tweet Owner
    echo $result->user->id;       // Tweet ID
    echo $result->text;              // Gets the Message of Tweet
    
    



In this way you can fetch Tweets of Search query data using Twitter API

Share this Post:

Facebook Google LinkedIn Tumblr Twitter

Like us on Facebook






More Posts






About us

We need to create new things in our lives at work or home. Many times we spend a lot of time to figure it out how. To help the netizens out there “to gain something” we illustrates clearly step by step how to gain some new things using articles.

Why us ?

We are not confined to any one category but we cover diverse topics from programming to personality management.

  • We have an expert team of authors who always try to deliver the best guidance for any topic
  • We always try to be the last stop in searching for the user.

Help us improve

  • Leave a comment here and we will surely try to make your experience better.

Learn here how to create anything new. You can be a contributer and help the people.