Get Twitter status on your site/blog

Updated the site and wanted to have a nice little Twitter box.  So I Googled “twitter status on site.” Nearly every result had the same twitter status function, which only retrieved status and removed (fixed?) hyperlinks.

I also wanted the time since the post and any hash tags to have a link to Twitter.  Also to remove the deprecated ereg_replace with preg_replace.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function get_status($twitter_id, $hyperlinks = true, $hash = true) {
    $twitter = Array();
    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitter_id.xml?count=1");
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    $src = curl_exec($c);
    curl_close($c);
    preg_match('/<created\_at>(.*)<\/created\_at>/', $src, $time);
    preg_match('/<text>(.*)<\/text>/', $src, $m);
   
    $status = htmlentities($m[1]);
    ($hyperlinks)? $status = preg_replace('/(http\:\/\/[\w\.\-\/]*\w)(?=.*)/','<a target="_blank" href="$1">$1</a>',$status) : $status = $status;
   
    $twitter['time'] = $time[0];
    ($hash)? $twitter['status'] = preg_replace('/\#(\w*)/','<a target="_blank" href="http://twitter.com/#!/search/$1">#$1</a>',$status) : $twitter['status'] = $status;
    return($twitter);
}

So examples!

1
2
3
4
5
6
7
8
9
//ALL
$twitter = get_status("HostGrenade");

print_r($twitter);

//HASH TAGS, BUT NOT HYPERLINKS
$twitter = get_status("HostGrenade",false,true);

print_r($twitter);




UPDATE : JANUARY 7, 2012

Working on a WordPress theme, I decided that this script needed to be updated to allow more than 1 status to come back. The function has changed as well as the call made to it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function get_status($twitter_id, $count = 3, $hyperlinks = true, $hash = true) {
        $twitter = Array();
        $c = curl_init();
        curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitter_id.xml?count=$count");
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        $src = curl_exec($c);
        curl_close($c);
        $xml = new SimpleXMLElement($src);
        $i=1;

        foreach($xml->status as $key=>$var){
            ($hyperlinks)? $var->text = preg_replace('/(http\:\/\/[\w\.\-\/]*\w)(?=.*)/','<a target="_blank" href="$1">$1</a>',$var->text) : $var->text = $var->text;
            ($hash)? $var->text = preg_replace('/\#(\w*)/','<a target="_blank" href="http://twitter.com/#!/search/$1">#$1</a>',$var->text) : $var->text = $var->text;
            $twitter[] = array("status" => $var->text,"time"=>elapsedtime(from_apachedate($var->created_at))); 
            if($i > $count){
                break;
            }
        }
        return($twitter);
    }

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>