I have the following code to request follower count from twitter api.
<?php
/*
* Requires the "Twitter API" wrapper by James Mallison
* to be found at https://github.com/J7mbo/twitter-api-php
*
* The way how to get a follower count was posted by Amal Murali
* on http://stackoverflow.com/questions/17409227/follower-count-number-in-twitter
*/
require_once('twitterapi/TwitterAPIExchange.php'); // adjust server path accordingly
// GET YOUR TOKENS AND KEYS at https://dev.twitter.com/apps/
$settings = array(
'oauth_access_token' => "SECRET", // enter your data here
'oauth_access_token_secret' => "SECRET", // enter your data here
'consumer_key' => "SECRET", // enter your data here
'consumer_secret' => "SECRET" // enter your data here
);
$ta_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=SECRET'; // enter your twitter name without the "@" here
$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'];
?>
It is working perfectly, but it only gets the follower count. I would also like the tweet count, following count, and like count.
According to the twitter api documentation the ones I need are all available from https://api.twitter.com/1.1/statuses/user_timeline.json. They are called:
Tweets = "statuses_count", Following = "friends_count", Likes = "favourites_count"
Problem is I can't work out how to adapt the code to call for all 4 variables. I did try just repeating the GET request and changing the variable names but it didn't seem to work.
Any help would be greatly appreciated!
UPDATE:
A big thank you to OldPadawan, his code worked perfectly.
This is what the final code looks like:
<?php
/*
* Requires the "Twitter API" wrapper by James Mallison
* to be found at https://github.com/J7mbo/twitter-api-php
*
* The way how to get a follower count was posted by Amal Murali
* on http://stackoverflow.com/questions/17409227/follower-count-number-in-twitter
*/
require_once('twitterapi/TwitterAPIExchange.php'); // adjust server path accordingly
// GET YOUR TOKENS AND KEYS at https://dev.twitter.com/apps/
$settings = array(
'oauth_access_token' => "xxxxx", // enter your data here
'oauth_access_token_secret' => "xxxxx", // enter your data here
'consumer_key' => "Xxxxx", // enter your data here
'consumer_secret' => "xxxxx" // enter your data here
);
$ta_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=ConsoleRepairHQ'; // enter your twitter name without the "@" here
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$follow_count=$twitter->setGetfield($getfield)
->buildOauth($ta_url, $requestMethod)
->performRequest();
$data = json_decode($follow_count, true);
foreach($data as $tweets) { // we run through the array
$followers = $tweets['user']['followers_count'];
$friends = $tweets['user']['friends_count'];
$likes = $tweets['user']['favourites_count'];
$statuses = $tweets['user']['statuses_count'];
} // end of loop
?>