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 http://twitter.com/status/user_timeline/YOUR_USERNAME.json. So if your twitter id is tareq_cse then the url will be http://twitter.com/status/user_timeline/tareq_cse.json. 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 http://twitter.com/status/user_timeline/tareq_cse.json?count=5

Now come to the php code.

<?php
$json = file_get_contents("http://twitter.com/status/user_timeline/tareq_cse.json?count=10", true); //getting the file content
$decode = json_decode($json, true); //getting the file content as array

echo "<pre>";
print_r($decode);
echo "</pre>";
?>

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

Array
(
    [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] => http://static.twitter.com/images/themes/theme9/bg.gif
                    [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

<?php
$json = file_get_contents("http://twitter.com/status/user_timeline/tareq_cse.json?count=10", true);
$decode = json_decode($json, true);

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

Lets pull some userdata

<?php
$json = file_get_contents("http://twitter.com/status/user_timeline/tareq_cse.json?count=10", 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

34 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("http://twitter.com/status/user_timeline/trafficscotland.json?count=5", true);
    $decode = json_decode($json, true);

    $count = count($decode); //counting the number of status
    for($i=0;$i<$count;$i++){

    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(“https://api.twitter.com/1/trends/1.json”);
    $json = json_decode($json, true);
    echo $json[0][‘name’];

  5. I’ve been trying this with

    file_get_contents(“https://stream.twitter.com/1/statuses/filter.json?locations=35.43,79.10,35.54,79.3″)

    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.

    Jose.

    • 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”];

      set_time_limit(0);
      $query_data = array(‘track’ => ‘apple, lady gaga’);
      $user = ‘username'; // replace with your account
      $pass = ‘password'; // replace with your account
      $fp = fsockopen(“ssl://stream.twitter.com”, 443, $errno, $errstr, 30);
      if(!$fp){
      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: stream.twitter.com\r\n”;
      $request .= “Authorization: Basic ” . base64_encode($user . ‘:’ . $pass) . “\r\n\r\n”;

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

      echo “Tweet: “;
      print_r($data);
      echo “”; //getting all the tweets
      $i++;
      if ($i>100) {
      break;
      }
      }

      }

      fclose($fp);
      }

      ?>

    •  @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(‘https://api.twitter.com/1/direct_messages.json?count=1&page=’, true);”
     
     

    $OAuth = new TwitterOAuth($consumer_key,$consumer_secret, $oAuthToken, $oAuthSecret);
     
    $direct_message = $OAuth = file_get_contents('https://api.twitter.com/1/direct_messages.json?count=1&page=', 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.

    http://twitter.com/status/user_timeline/tareq_cse.json?count=5

    I get

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

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

  9. Not working at all

    Warning: file_get_contents(http://twitter.com/status/user_timeline/GurubakshGulati.json?count=10) [function.file-get-contents]: failed to open stream: Unable to find the socket transport “ssl” – did you forget to enable it when you configured PHP? in D:\xampp\htdocs\projects\twitter\2.php on line 2

    Notice: Use of undefined constant user – assumed ‘user’ in D:\xampp\htdocs\projects\twitter\2.php on line 5

    Notice: Use of undefined constant profile_image_url – assumed ‘profile_image_url’ in D:\xampp\htdocs\projects\twitter\2.php on line 5
    Notice: Use of undefined constant user – assumed ‘user’ in D:\xampp\htdocs\projects\twitter\2.php on line 7

    Notice: Use of undefined constant name – assumed ‘name’ in D:\xampp\htdocs\projects\twitter\2.php on line 7
    Name:

    Notice: Use of undefined constant user – assumed ‘user’ in D:\xampp\htdocs\projects\twitter\2.php on line 8

    Notice: Use of undefined constant url – assumed ‘url’ in D:\xampp\htdocs\projects\twitter\2.php on line 8
    Web:
    Notice: Use of undefined constant user – assumed ‘user’ in D:\xampp\htdocs\projects\twitter\2.php on line 10

    Notice: Use of undefined constant location – assumed ‘location’ in D:\xampp\htdocs\projects\twitter\2.php on line 10
    Location:

    Notice: Use of undefined constant user – assumed ‘user’ in D:\xampp\htdocs\projects\twitter\2.php on line 11

    Notice: Use of undefined constant statuses_count – assumed ‘statuses_count’ in D:\xampp\htdocs\projects\twitter\2.php on line 11
    Updates:

    Notice: Use of undefined constant user – assumed ‘user’ in D:\xampp\htdocs\projects\twitter\2.php on line 12

    Notice: Use of undefined constant followers_count – assumed ‘followers_count’ in D:\xampp\htdocs\projects\twitter\2.php on line 12
    Follower:

    Notice: Use of undefined constant user – assumed ‘user’ in D:\xampp\htdocs\projects\twitter\2.php on line 13

    Notice: Use of undefined constant friends_count – assumed ‘friends_count’ in D:\xampp\htdocs\projects\twitter\2.php on line 13
    Following:

    Notice: Use of undefined constant user – assumed ‘user’ in D:\xampp\htdocs\projects\twitter\2.php on line 14

    Notice: Use of undefined constant description – assumed ‘description’ in D:\xampp\htdocs\projects\twitter\2.php on line 14
    Description:

Leave a Reply