Number of counters on Twitter

How to count the number of my subscribers using PHP.

I found this answer here: Twitter follower number , but it doesn’t work because API 1.0 is no longer active.

I also tried using API 1.1 with this url: https://api.twitter.com/1.1/users/lookup.json?screen_name=google , but an error is displayed (bad authentication data).

Here is my code:

$data = json_decode(file_get_contents('http://api.twitter.com/1.1/users/lookup.json?screen_name=google'), true); echo $data[0]['followers_count']; 
+8
php twitter twitter-follow
source share
2 answers

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.

Use this instead:

 <?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=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; ?> 

In some cases, XML parsing may be easier.

Here's the solution (tested):

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

Hope this helps!

+13
source share

If it is without auth (replace "stackoverflow" with user)

 $.ajax({ url: "https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=stackoverflow" dataType : 'jsonp', crossDomain : true }).done(function(data) { console.log(data[0]['followers_count']); }); 

with php

 $tw_username = 'stackoverflow'; $data = file_get_contents('https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names='.$tw_username); $parsed = json_decode($data,true); $tw_followers = $parsed[0]['followers_count']; 
+14
source share

All Articles