3 min read

PHP snippets for Twitter integration

Twitter is an essential tool for website owners, and you should definitely integrate it into your site if you want to attract more traffic and new visitors. Today, we will look at some PHP code snippets for interacting with Twitter.

Fetching the Number of Followers

To display the number of followers for a specific account, use the following function:

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

Usage example:

$followers = get_followers('AlexSchedrov');
echo 'Alex Schedrov has ' . $followers . ' followers.';

Fetching the Latest Status

Using PHP and cURL, you can easily retrieve the latest tweet from a user:

function get_status($twitter_id, $hyperlinks = true) {
  $c = curl_init();
  curl_setopt($c, CURLOPT_URL, "https://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 = $m[1];
  if ($hyperlinks) {
    $status = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", '<a href="\0">\0</a>', $status);
  }
  return $status;
}

Usage example:

echo get_status('AlexSchedrov');

Getting the Number of Retweets

Most bloggers use the Tweetmeme widget to display the number of retweets for their posts. Its API can also be used to retrieve the retweet count for a specific URL.

The following function will fetch the number of retweets, with the URL passed as a parameter:

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

Usage example:

echo tweetCount('https://alexschedrov.com');

Shortening URLs with TinyURL

As you know, if you are a Twitter user, you cannot send messages longer than 140 characters. To avoid this issue, you should use shortened URLs. There are many different URL shortening services available online. TinyUrl.com is one of them, and one of its advantages is that you don’t need an account to use it with PHP.

The following function takes a long URL as a parameter and returns a shortened URL using the TinyURL Shortener.

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

Usage example:

$short_url = getTinyUrl('https://alexschedrov.com');

Shortening URLs with Bit.ly

In the previous code snippet of this article, I showed you how to shorten your URLs using TinyUrl.com. That’s great, but I’m sure some of you prefer using the bit.ly service. No problem, you can still use PHP to generate shortened URLs.

function bitly($url) {
  $content = file_get_contents("https://api.bit.ly/v3/shorten?login=YOURLOGIN&apiKey=YOURAPIKEY&longUrl=" . $url . "&format=xml");
  $element = new SimpleXmlElement($content);
  $bitly = $element->data->url;
  return $bitly ? $bitly : '0';
}

Replace YOURLOGIN with your Bit.ly username and YOURAPIKEY with your API key, which can be found in your profile settings on Bit.ly.

Usage example:

$short_url = bitly('https://alexschedrov.com');