0

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

3 Answers 3

1

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'];

Sign up to request clarification or add additional context in comments.

1 Comment

hmm, makes sence, but im still having issues. Not sure what exactly is causing this. problem for me I either get NULL or nothing at all.
1

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'];

4 Comments

Still getting NULL Even if i do var_dump(json_decode($json, true));
$json = file_get_contents('cdn.syndication.twimg.com/widgets/followbutton/…); $data = json_decode($json,true); $twitcount = $data[0]['followers_count']; echo '<pre>'; print_r($data); print_r($twitcount);
try to use this code this will help cheers.. and make sure your json variable has data try to echo $json before decode it.
Thank you all the answers worked acctually. Just had some conflicts with file_get_contents Ended up going with a CURL solution to this.
0

$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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.