Mic
Mic

Reputation: 333

Print Value from JSON URL with php twitter

Im trying to print values from a json file to my website. I realise there is a API system for twitter how ever for the intended purpose i dont feel its needed to apply for a twitter api and wait just for a follower count status.

im not sure why. but nothing is displayed. Is there something im missing?

Here is my current code

$json = file_get_contents('https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=stackoverflow');

$data = json_decode($json,true);

$twitcount = $data['followers_count'][0];

echo "<b>";
print_r($twitcount);

Visting the url below will push a json file with basic infomation about the twitter account. https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=stackoverflow

Upvotes: 0

Views: 142

Answers (3)

Mic
Mic

Reputation: 333

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=stackoverflow');
$result = curl_exec($ch);
curl_close($ch);


$data = json_decode($result,true);
$twitcount = $data[0]['followers_count'];
print_r($twitcount);

All your answers where correct and great thank you

I ended up setting it up with CURL instead. Seems like iv got some issues with file_get_contents

Maybe some sort of conflict.

Upvotes: 0

Akhilesh
Akhilesh

Reputation: 968

correct your index

Array ( [0] => Array ( [following] => [id] => 128700677 [screen_name] => StackOverflow [name] => Stack Overflow [protected] => [followers_count] => 55358 [formatted_followers_count] => 55.4K followers [age_gated] => )

)

$twitcount = $data[0]['followers_count'];

Upvotes: 1

Terence Eden
Terence Eden

Reputation: 14304

If you run var_export($data); you'll get:

array (
  0 => 
  array (
    'following' => false,
    'id' => '128700677',
    'screen_name' => 'StackOverflow',
    'name' => 'Stack Overflow',
    'protected' => false,
    'followers_count' => 55359,
    'formatted_followers_count' => '55.4K followers',
    'age_gated' => false,
  ),
)

So your code needs to be $twitcount = $data[0]['followers_count'];

Upvotes: 1

Related Questions