0

This is the code to grab tweets, but i need this in PHP, can anybody offer any insight?

$(document).ready( function() {

            var url = "http://twitter.com/status/user_timeline/joebloggs.json?count=1&callback=?";
            $.getJSON(url,
            function(data){
                $.each(data, function(i, item) {
                    $("#twitter-posts").append("<p>" + item.text.linkify() + " <span class='created_at'>" + relative_time(item.created_at) + " via " + item.source + "</span></p>");
                });
            });
        });

        String.prototype.linkify = function() {
            return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
        return m.link(m);
      });
     }; 
      function relative_time(time_value) {
          var values = time_value.split(" ");
          time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
          var parsed_date = Date.parse(time_value);
          var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
          var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
          delta = delta + (relative_to.getTimezoneOffset() * 60);

          var r = '';
          if (delta < 60) {
            r = 'a minute ago';
          } else if(delta < 120) {
            r = 'couple of minutes ago';
          } else if(delta < (45*60)) {
            r = (parseInt(delta / 60)).toString() + ' minutes ago';
          } else if(delta < (90*60)) {
            r = 'an hour ago';
          } else if(delta < (24*60*60)) {
            r = '' + (parseInt(delta / 3600)).toString() + ' hours ago';
          } else if(delta < (48*60*60)) {
            r = '1 day ago';
          } else {
            r = (parseInt(delta / 86400)).toString() + ' days ago';
          }

          return r;
    }
    function twitter_callback ()
    {
        return true;
    }
6
  • Yes, it can be replicated in PHP. What have you tried? Just do what it does. The only "issue" may be if the sites require cookies -- set in a web-browser client, but perhaps not by the PHP code -- to serve up the content. Commented Jan 14, 2011 at 20:52
  • with? any specific functions you could point me to Commented Jan 14, 2011 at 20:53
  • @benhowdle89 Exactly which part is "giving problems"? Numerous functions and operators will ultimately be used, just as they are in JS. Are you asking "how can I download a web resource" in PHP? Or is there something else? Commented Jan 14, 2011 at 20:54
  • The fact that this code preserves twitter hashtags, hyperlinks, i thought the PHP would just bring back dumb text Commented Jan 14, 2011 at 20:56
  • @benhowdle89 JSON is "dumb text" :-) That is why you use a JSON-library/function to turn it into a structure to manipulate. jQuery does this automatically: "dumb text" -> JavaScript object(s). In PHP it'd just be "dumb text" -> some PHP object. Commented Jan 14, 2011 at 20:58

3 Answers 3

2

The most complicated part is the request to twitter, you can do something like that:

$content = file_get_contents("http://twitter.com/status/user_timeline/joebloggs.json?count=1&callback=?");

$twitter_response = json_decode($content);

foreach($twitter_response as $item){
      //format someway
      $item->text; #get the text of each tweet

}
Sign up to request clarification or add additional context in comments.

Comments

2

You can use cURL to access the file. Then use the PHP function json_decode to work with the data.

PHP: cURL
PHP: json_decode

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://twitter.com/status/user_timeline/joebloggs.json?count=1&callback=?");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);

$json = json_decode($result);
var_dump($json);

2 Comments

thanks Michael, would this preserve Twitter hashtags or hyperlinks?
No idea, to be completely honest. That code is theoretical; I have not tested it. But, I've done similar requests in the past from other sites.
1

Yes you can do it. For the major parts you can use cURL or file_get_contents to fetch the data, json_decode to parse it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.