Categories
Games PHP Web Trends

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.

<?php 
require_once( 'TwitterAPIExchange.php' ); //get it from https://github.com/J7mbo/twitter-api-php

/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
	'oauth_access_token' 		=> "YOUR_OAUTH_ACCESS_TOKEN",
	'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET",
	'consumer_key' 				=> "YOUR_CONSUMER_KEY",
	'consumer_secret' 			=> "YOUR_CONSUMER_SECRET"
);

$ta_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=SwatantraKumar'; //'?screen_name=REPLACE_ME';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange( $settings );
$follow_count = $twitter->setGetfield( $getfield )
						->buildOauth( $ta_url, $requestMethod )
						->performRequest();
$data = json_decode( $follow_count, true );
$followers_count = $data[0]['user']['followers_count'];
echo $followers_count;
?>

Parsing the XML

<?php 
$xml = new SimpleXMLElement( urlencode( strip_tags( 'https://twitter.com/users/google.xml' ) ), null, true );
echo "Follower count: ".$xml->followers_count;
?>
'Coz sharing is caring

By Swatantra Kumar

Swatantra is an engineering leader with a successful record in building, nurturing, managing, and leading a multi-disciplinary, diverse, and distributed team of engineers and managers developing and delivering solutions. Professionally, he oversees solution design-development-delivery, cloud transition, IT strategies, technical and organizational leadership, TOM, IT governance, digital transformation, Innovation, stakeholder management, management consulting, and technology vision & strategy. When he's not working, he enjoys reading about and working with new technologies, and trying to get his friends to make the move to new web trends. He has written, co-written, and published many articles in international journals, on various domains/topics including Open Source, Networks, Low-Code, Mobile Technologies, and Business Intelligence. He made a proposal for an information management system at the University level during his graduation days.

One reply on “Get twitter follower count in php”

Hello,

I’m new with php and I would like to ask in which files do I have to add the code above.

Thanks for your help

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.