Tag Archives: twitter

Get twitter follower count in php

Twitter API 1.0 is deprecated and is no longer active. With the REST 1.1 API, you need oAuth authentication to retrieve data from Twitter.

<br />
&lt;?php<br />
require_once( 'TwitterAPIExchange.php' ); //get it from https://github.com/J7mbo/twitter-api-php</p>
<p>/** Set access tokens here - see: https://dev.twitter.com/apps/ **/<br />
$settings = array(<br />
	'oauth_access_token' 		=&gt; &quot;YOUR_OAUTH_ACCESS_TOKEN&quot;,<br />
	'oauth_access_token_secret' =&gt; &quot;YOUR_OAUTH_ACCESS_TOKEN_SECRET&quot;,<br />
	'consumer_key' 				=&gt; &quot;YOUR_CONSUMER_KEY&quot;,<br />
	'consumer_secret' 			=&gt; &quot;YOUR_CONSUMER_SECRET&quot;<br />
);</p>
<p>$ta_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';<br />
$getfield = '?screen_name=SwatantraKumar'; //'?screen_name=REPLACE_ME';<br />
$requestMethod = 'GET';<br />
$twitter = new TwitterAPIExchange( $settings );<br />
$follow_count = $twitter-&gt;setGetfield( $getfield )<br />
						-&gt;buildOauth( $ta_url, $requestMethod )<br />
						-&gt;performRequest();<br />
$data = json_decode( $follow_count, true );<br />
$followers_count = $data[0]['user']['followers_count'];<br />
echo $followers_count;<br />
?&gt;<br />

Parsing the XML

<br />
&lt;?php<br />
$xml = new SimpleXMLElement( urlencode( strip_tags( 'https://twitter.com/users/google.xml' ) ), null, true );<br />
echo &quot;Follower count: &quot;.$xml-&gt;followers_count;<br />
?&gt;<br />

'Coz sharing is caring