Skip to content

Instantly share code, notes, and snippets.

@shamess
Created April 17, 2012 14:07
Show Gist options
  • Save shamess/2406181 to your computer and use it in GitHub Desktop.
Save shamess/2406181 to your computer and use it in GitHub Desktop.
Loop over some tweets, in PHP
<?php
$twitter_user = "shamess";
$json = file_get_contents ("http://api.twitter.com/1/statuses/user_timeline.json?screen_name=".$twitter_user."&count=3&trim_user=true");
$tweets = json_decode ($json);
echo "<ul>";
foreach ($tweets as $tweet) {
$tweet_text = $tweet->text;
// change all the urls to links
$tweet_text = preg_replace ("!http://(\\S+)!i", "<a href=\"http://$1\">http://$1</a>", $tweet_text);
// @usernames
$tweet_text = preg_replace ("/@(\\w+)/", "<a href=\"http://twitter.com/$1\">@$1</a>", $tweet_text);
// #hashtags too
$tweet_text = preg_replace ("/#(\\w+)/", "<a href=\"http://twitter.com/search?q=$1\">#$1</a>", $tweet_text);
echo "<li>".$tweet_text." - <em><a href=\"http://twitter.com/shamess/status/".$tweet->id_str."\">".date ('l \a\t H:i', strtotime ($tweet->created_at))."</a></em></li>";
}
echo "</ul>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment