Playing with twitter JSON using PHP

35 thoughts on “Playing with twitter JSON using PHP”

  1. very nice bit of code
    how do you get it to have working hyper links from the tweet when it displays on your page?
    any help on this would be great

  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][text][/text]."";
    }

    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.

    1. 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);
      }

      ?>

    1.  @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);”
     
     
    [code]
    $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
    [/code]
     
     

  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

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.