PHP snippets to interact with Twitter

Twitter is an awesome tool for website owners, and you should definitely integrate it into your website if you want to attract more traffic and new visitors. Today, let’s take a look at some PHP snippets to interact with Twitter.

Get number of Twitter followers

Have you seen my blog sidebar? I display the number of followers I have in full text. This is actually pretty easy to do. The first thing you need is this function:

function get_followers($twitter_id){
	$xml=file_get_contents('http://twitter.com/users/show.xml?screen_name='.$twitter_id);
	if (preg_match('/followers_count>(.*)</',$xml,$match)!=0) {
		$tw['count'] = $match[1];
	}

	return $tw['count'];
}
$nb =  get_followers('phpsnippets');
echo "PHP Snippets already have ".$nb." followers!";
Sources: http://www.phpsnippets.info/get-twitters-followers-in-php

Get latest Twitter status

Using PHP and cURL, it is pretty easy to get the status of a specific user. Once you have it, what about displaying it on your blog, like I do in WPRecipes footer?

Twitter is extremely popular these days, and many clients already asked me to display their status on their website. Doing so is quite easy using cURL. This snippet will return the status of any Twitter user.

function get_status($twitter_id, $hyperlinks = true) {
    $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('/<text>(.*)<\/text>/', $src, $m);
    $status = htmlentities($m[1]);
    if( $hyperlinks ) $status = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", '<a href="%5C%22%5C%5C0%5C%22">\\0</a>', $status);
    return($status);
}

Usage

Simply use the twitter name of the person you’d like to retrieve the status:

echo get_status('phpsnippets');

The function accept a second parameter, $hyperlinks. If set to false, links will be displayed as text. If you’re on Twitter, you should definitely add PHP Snippets so you’ll not miss any of our snippets!

Sources: http://www.phpsnippets.info/get-twitter-status-using-php

Link to update status, without encoding problems

Many websites and blogs show you how to create a link to Twitter that will update your status. But unfortunely, most websites don’t explain what you need to do in order to avoid encoding problems of spaces and special characters.

<a href="http://twitter.com?status=@username Message will be here?">Tweet!</a>

Get number of retweets for a specific page

Most bloggers are using the Tweetmeme widget to display the number of retweets of their posts. Did you know that Tweetmeme also has an API you can use to get how many times a specific url has been retweeted?

How many times an url have been retweeted? This is a good question, and the answer is quite easy to get using PHP and the Tweetmeme API. Here is a ready to use function:

function tweetCount($url) {
    $content = file_get_contents("http://api.tweetmeme.com/url_info?url=".$url);
    $element = new SimpleXmlElement($content);
    $retweets = $element->story->url_count;
    if($retweets){
        return $retweets;
    } else {
        return 0;
    }
}

Usage

Nothing complicated. Just use echo to display how many times an url have been retweeted:

echo tweetCount('http://www.phpsnippets.info');

Sources: http://www.phpsnippets.info/get-how-many-times-a-page-have-been-retweeted-using-php

Get twitter status using PHP

Twitter is extremely popular these days, and many clients already asked me to display their status on their website. Doing so is quite easy using cURL. This snippet will return the status of any Twitter user.

Usage

Simply use the twitter name of the person you’d like to retrieve the status:

echo get_status('phpsnippets');

The function accept a second parameter, $hyperlinks. If set to false, links will be displayed as text.

Convert url to TinyURL

Sharing long urls by email or on social media sites is never a good idea, this is why most people are using urls shorteners such as biy.ly or Tinyurl. This function takes a single parameter, an url, and will return a short url using the Tinyurl service.

function getTinyUrl($url) {
    return file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
}

Usage

Nothing complicated, simply pass an url to the function to get its short url:

$my_url = getTinyUrl('http://www.phpsnippets.info/test-existence-of-a-given-url-with-curl');
echo 'Short url: '.$my_url;

If you want to use this snippet within WordPress, you can do so by using the following within the loop:

getTinyUrl(get_permalink($post->ID));