Playing with twitter JSON using PHP

Now twitter is playing a major roll to our day. Many of us is using twitter. Now lets play something with it. Today i will use JSON to play with twitter LOL.

Lets play with our statuses. If you are using twitter, then your status url is So if your twitter id is tareq_cse then the url will be Now i will add a extra parameter here and this is count. This url shows the last 20 status update of yours. So if i need only 5 update then? Then we will add a count variable there. So the url will be

Now come to the php code.

$json = file_get_contents("", true); //getting the file content
$decode = json_decode($json, true); //getting the file content as array

echo "<pre>";
echo "</pre>";

Now run the code and see. You will get output like this

    [0] => Array
            1 => is digging wordpress
            [in_reply_to_user_id] =>
            [user] => Array
                    [notifications] =>
                    [time_zone] => Dhaka
                    [description] => A blogger, microblogger and learner
                    [following] =>
                    [utc_offset] => 21600
                    [profile_text_color] => 666666
                    [location] => Rajshahi, Bangladesh
                    [id] => 15842888
                    [profile_background_image_url] =>
                    [profile_link_color] => 2FC2EF

            [favorited] =>
            [in_reply_to_screen_name] =>
            [truncated] =>
            [created_at] => Sun May 03 20:56:00 +0000 2009
            [id] => 1689697302
            [in_reply_to_status_id] =>
            1 => TwitterFox

Now get some usefull data from this. If we need the last 10 twitter update write the code

$json = file_get_contents("", true);
$decode = json_decode($json, true);

echo "<pre>";
$count = count($decode); //counting the number of status
echo $decode[$i]1."<br>";
echo "</pre>";

Lets pull some userdata

$json = file_get_contents("", true);
$decode = json_decode($json, true);

echo "<img src=\"".$decode[0][user][profile_image_url]."\"
"; //getting the profile image
echo "Name: ".$decode[0][user][name]."<br>"; //getting the username
echo "Web: ".$decode[0][user][url]."
"; //getting the web site address
echo "Location: ".$decode[0][user][location]."<br>"; //user location
echo "Updates: ".$decode[0][user][statuses_count]."<br>"; //number of updates
echo "Follower: ".$decode[0][user][followers_count]."<br>"; //number of followers
echo "Following: ".$decode[0][user][friends_count]."<br>"; // following
echo "Description: ".$decode[0][user][description]."<br>"; //user description

Following this method, you can pull any data by analyzing the first output

33 thoughts on “Playing with twitter JSON using PHP

  1. Pingback: Playing with twitter JSON using PHP | Tareq's Planet - 【23php】

  2. Great tutorial, simple but powerful.

    However, what if twitter’s api fails? Have you got a fall back to stop your whole website failing due to the twitter drop out?

    I think by making the twitter json parser script a function then running it in an if script would work?

    like so:

    function read_twit() {
    $json = file_get_contents("", true);
    $decode = json_decode($json, true);

    $count = count($decode); //counting the number of status

    echo "".$decode[$i]."";

    And then calling it into your website like so:

    Any thoughts?

  3. This code may not be working in live server. If your page permission is not set to “777″ then you can’t directly access the page using “file_get_contents” on the fly.

  4. which is the error?????????

    $json = file_get_contents(“”);
    $json = json_decode($json, true);
    echo $json[0]['name'];

  5. I’ve been trying this with


    but file_get_contents doesn’t appear to be returning anything. Not even a “false.”

    Any idea what might be wrong?

  6. Great code, very easy to implement in my project. I am having an issue though with using multiple keywords. It will only stream the first keyword. I’ve attempted using “keyword1, keyword2″ also “keyword1 OR keyword2″, neither has worked for me. Is there anything else I am missing, I looked through all the developer documentation as well as many websites and cannot find an example of submitting multiple keywords, although it clearly states you can submit 200 keywords at a time. I am using the same exact code as you have above only modifying the output to my webpage. Any help would be greatly appreciated.


    • Whoops, sorry, I mistakenly posted that I was using the same code, actually it’s modified for the streaming api. Here is the code I am using:

      $keyword = $_POST["keyword"];

      $query_data = array(‘track’ => ‘apple, lady gaga’);
      $user = ‘username’; // replace with your account
      $pass = ‘password’; // replace with your account
      $fp = fsockopen(“ssl://”, 443, $errno, $errstr, 30);
      print “$errstr ($errno)\n”;
      } else {
      $request = “GET /1/statuses/filter.json?” . http_build_query($query_data) . ” HTTP/1.1\r\n”;
      //$request = “GET /1/account/rate_limit_status.json?” . http_build_query($query_data) . ” HTTP/1.1\r\n”;
      $request .= “Host:\r\n”;
      $request .= “Authorization: Basic ” . base64_encode($user . ‘:’ . $pass) . “\r\n\r\n”;

      fwrite($fp, $request);
      //echo “Keywords: $keyword “;
      $json = fgets($fp);
      $data = json_decode($json, true);
      // Do something with the data!

      echo “Tweet: “;
      echo “”; //getting all the tweets
      if ($i>100) {




    •  @Infobot2012 I am not sure if you can get all the tweets from twitter (maybe last 3200 tweets), there isn’t a easy way around. You can search for it.

  7. Getting error message “Warning: file_get_contents() [function.file-get-contents]: Unable to find the wrapper “https” – did you forget to enable it when you configured PHP” in relation to line “$direct_message = $OAuth = file_get_contents(‘’, true);”

    $OAuth = new TwitterOAuth($consumer_key,$consumer_secret, $oAuthToken, $oAuthSecret);
    $direct_message = $OAuth = file_get_contents('', true);
    $decode = json_decode($direct_message, true); // this gets the json file content as an array


  8. Hi,

    Thanks so much for posting this. I’m finding it to be very useful for a twitter-based project that I’m working on right now. I was able to get this working a couple months ago. However, I’ve recently revisited it and am having problems. The JSON link doesn’t seem to be working properly anymore.

    I get

    message: “Sorry, that page does not exist”,
    code: 34

    Any idea what’s going on? I’m at a loss.

Leave a Reply