Twitter API Script For PHP

A project I am working on requires me to pull in the last tweets made by the user logged in and display them to screen. The user does not have to be able to update their twitter status or anything else besides view his/her latest tweets (lets say last 10 tweets).

Instead of reinventing the wheel, I would like to know of any usefull API scripts to achieve this.

What do you use?

Any information would be appreciated.

Here’s a quick snippet for you. :slight_smile:


<?php
error_reporting(-1);
ini_set('display_errors', true);

$user = new SimpleXMLElement(
  'http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=AnthonySterling&count=10',
  null,
  true
);

foreach($user->status as $status){
  echo $status->text . "\
";
}
?>

Exactly what I wanted. :slight_smile:

Thanks.

Ok, so now things got a bit more complicated. I have to be uble to pull in the tweets of 5 configured usernames.

My requirements are thus to display the 15 latest tweets of the the combined tweets of 5 configured users.

I checked the twitter list widget on the twitter site, but this widget breaks on Google Chrome.

I also checked out the twitter API, but can’t find a solution. Can anyone point me in the right direction?

Sure.


<?php
$usernames = array(
  'AnthonySterling',
  'PhilSherry',
  'CraigTweedy',
  'Onion2k',
  'TimIgoe',
);

$tweets = array();

foreach($usernames as $user){
  
  $twitter = new SimpleXMLElement(
    'http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=' . $user . '&count=3',
    null,
    true
  );
  
  foreach($twitter->status as $tweet){
    array_push(
      $tweets,
      array(
        'name'  => (string)$tweet->user->name,
        'nick'  => (string)$tweet->user->screen_name,
        'when'  => strtotime($tweet->created_at),
        'text'  => (string)$tweet->text,
      )
    );
  }
  
}

uasort(
  $tweets,
  create_function(
    '$a, $b',
    'if($a["when"] === $b["when"]) return 0; return ($a["when"] < $b["when"]) ? 1 : -1 ;'
  )
);

print_r(
  $tweets
);

/*
  [name] => Anthony Sterling
  [nick] => AnthonySterling
  [when] => 1289847169
  [text] => has been emailed by @philsherry.

*/
?>

Thanx Sir, that did the trick. It was a bit slow so I added some caching and vooila!

Excellent news!

Yep, caching tends to do that. :slight_smile: