0

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

?>
Chris
  • 431
  • 5
  • 11
  • 18

1 Answers1

1

Once you get the json response from Twitter, you treat it as an array and use foreach

In your case, after :

$data = json_decode($follow_count, true);

You can print the array to see what it looks like (just for information, not needed later) but it makes it easier to understand the logic and where data is stored (can be different array structure)

What you need, is to add this :

<?php

$data = json_decode($follow_count, true);
print_r($data); // just checking, not needed later

  foreach($data as $tweets) { // we run through the array

    // here's an example of the structure and how you access it
    // compare to the printed array if you need more information

    $tweet = $tweets['text'];
    $name = $tweets['user']['name'];
    $turl = $tweets['user']['url'];
    $followers = $tweets['user']['followers_count'];
    $friends = $tweets['user']['friends_count'];
    $likes = $tweets['user']['favourites_count'];

    echo "Tweet: $tweet ($name / $turl) -> followed by : $followers -> friends: $friends -> likes: $likes<br />";
  } // end of loop

?>

And you're done. Hope this helps :)

OldPadawan
  • 1,247
  • 3
  • 16
  • 25
  • Massive thanks OldPadawan. I was trying for hours to get this working. I have updated the original question with the final code if you want to have a look. Thanks again! – Chris May 11 '17 at 15:36