≡ Menu
Deep in the Code

Getting the Number of Twitter Followers Without Using the Twitter API

Since Twitter upgraded the Twitter REST API to version 1.1 earlier this year, a simple HTTP query against the API no longer works. You now have to include authentication information which would require redevelopment of many programs that perform this previously simple action.

I wanted to include the number of Twitter followers on this blog using the Social Impact Widget, which was made for version 1 of the Twitter API.

Using the YQL language API from Yahoo, this information can still be retrieved by Web scraping, without resorting to using Twitter’s API. Aakash Chakravarthy’s article explains this method well.

By using this method, the PHP code in the Social Impact Widget can be modified to pull the number of Twitter followers via Web scraping.

The original PHP code for the widget that retrieves the Twitter data:

$return_Twitter = $this->_helper_curl(sprintf('https://api.twitter.com/1/users/show.json?screen_name=%1$s',
	), $this->var_sUserAgent);

Remove this code and add the below, also making some changes to the try block immediately below it to change the way it stores the returned data:

$return_Twitter = $this->_helper_curl('http://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20from%20html%20where%20url=%22http://twitter.com/' . $var_sTwitterId . '%22%20AND%20xpath=%22//a[@class='js-nav']/strong%22&format=json'                                
                    , $this->var_sUserAgent); // Opening the Query URL
	try {
		$obj_TwitterData = json_decode($return_Twitter);
		if($obj_TwitterData) {
			if(!empty($obj_TwitterData->query->results->strong[2])) {
				$this->array_Options[$this->var_sArrayOptionsKey]['twitter-count'] = intval(str_replace(',', '', $obj_TwitterData->query->results->strong[2]));
			} // END if(!empty($obj_TwitterData->query->results->strong[2]))
		} // ENDif($obj_TwitterData)
	} catch(Exception $e) {
		$this->array_Options[$this->var_sArrayOptionsKey]['twitter-count'] = (int) $var_iTwitterFollowerCount;

And now, your widget will appear correctly! The caveat here is that if Twitter ever changes the layout of the site, then the line


will have to be modified accordingly.