On 11th June 2013 Twitter completely deprecated the version 1 of its REST API. The new API 1.1 requires OAuth credentials like consumer key, Access token and all other necessary keys to access the twitter data. I used the v1 API for retrieving the my twitter follower count. But in version 1.1 without OAuth it is possible to obtain any data. It gives the below message when any HTTP request is made without proper Authentication.

{"errors":[{"message":"Bad Authentication data","code":215}]}

The OAuth keys can be generated in the Twitter developers page. But still there is another way to retrieve the basic regularly needed information like “follower” count without any OAuth keys needed.

This can be done by using the Yahoo Query Language (YQL: A tool useful to retrieve data from any Web documents with a HTTP request). The actual idea is to parse the “follower” count from the twitter profile page. For example my twitter profile page https://twitter.com/vaakash displays the followers count.

twitter-follower-count-profile

Using YQL a query is made to that page and the count text is parsed out using XPATH. When the source of the page is viewed, it is seen that the count is wrapped in an anchor tag and a strong tag like the below.

<a class="js-nav" href="/followers" data-element-term="follower_stats" data-nav='followers'>  
<strong>277</strong> Followers
</a>

The next step is to create the YQL query in the YQL’s official console page. The query to parse all the html of the profile page is,

SELECT * FROM html 
WHERE url="https://twitter.com/vaakash"

Inorder to traverse through parsed html tree, XPATH is used. Since the count is wrapped in a anchor and strong tag the XPATH to grab the count is

//a[@class='js-nav']/strong

This XPATH query is used along with the YQL query joined the AND verb to specifically select the twitter count. The final YQL query is,

SELECT * FROM html
WHERE url="https://twitter.com/vaakash"
AND xpath="//a[@class='js-nav']/strong" 

Running this query after selecting the JSON output format, the console window displays the parsed results. The count is actually present in the query->results->strong->2. Since there are similar texts wrapped in a similar anchor and strong fashion, two more addional results which include the “tweets posted” and “following” count. These can also be used if needed.

Now the direct URL of the JSON output is displayed in the bottom of the page. It can be used any application to get the follower count.

Using the Query in PHP application

The Query URL is used in a PHP application as below.

$data = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20from%20html%20where%20url=%22http://twitter.com/vaakash%22%20AND%20xpath=%22//a[@class='js-nav']/strong%22&format=json"); // Opening the Query URL
$data = json_decode($data); // Decoding the obtained JSON data

$count = intval($data->query->results->strong[2]); // The count parsed from the JSON
echo "No of twitter followers: " . $count; // Printing the count

A live example can be seen in the sidebar on the right. The variable data holds the below value.

twitter-follower-count-array

Note:

Using this method it is easy to parse out the twitter follower count but difficult for further more GET related twitter services. The above method is just an example to demonstrate the advantage of YQL. Since YQL caches the query to a good extent, it is an advantage for some applications. The best method to use Twitter’s v1.1 REST APIs is creating an OAuth account and implementing them as suggested by Twitter.

Subscribe for updates

Get updates on the WordPress plugins, tips and tricks to enhance your WordPress experience. No spam. View newsletter

Add your comment 10 Comments so far