Script doesnt work

hi ! I wrote this script but I get nothing after “bio” … but why ? :confused:

<?php $url = ''; $json = file_get_contents($url); $data = json_decode($json, true); $bio = $data['bio']; echo "bio: $bio"; ?>

whats wrong ?

The users endpoint has been known to sometimes return the v2 response when v3 should be returned. v2 did not have bio.

As a side-note, if this is on an HTML page, be sure to use htmlspecialchars($bio) to prevent cross-site scripting.

no .php .so what have I to change ?

There’s nothing you can do but wait for the server-side cache to expire, if the bio is missing in the response.

so the problem is by twitch ?

how do I get the number of followers of a streamer? :confused:

but this doesn’t work !

> <?php
> $url = '';
> $json = file_get_contents($url);
> $data = json_decode($json, true);
> $followers = $data['followers'];
> echo "followers: $followers";
> ?>

but why ??

You should STOP using file_get_contents and use cURL instead.

It’s possible that the file_get_contents is failing and you are not checking the response for it.

Further more you need to be sending a Client-ID in the header, as per the Twitch API Rules of the Road, which you just can’t do with file_get_contents

So what instead of file_get_contents ?

Check out this function:

Easily replace file_get_contents with cURL.

so, so ?

$url = '';
$json = cURL($url);
$data = json_decode($json, true);
$followers = $data['followers'];
echo "followers: $followers";

doesn’t work :confused:

@Dersarius: The example that @matt_thomas posted is a full function that adds the file_get_contents_curl function. You would need to include his entire function in order to then use file_get_contents_curl($url). Right now, you’re not including the function (that I can see) and you’re just calling cURL($url), which isn’t the name of the function.

Having said that, you should really just learn how to use cURL. That will help you immensely in the long run for all APIs if you’re going to use PHP. Matt’s abstraction is a huge help, but you should understand what it is doing. If you’re new to PHP, I would recommend PHP The Right Way or PHP on CodeAcademy.

1 Like

so how should the code look like ? :frowning:

I’ll point to my previous post about learning how to use cURL and the two learning resources: PHP The Right Way and the PHP class on CodeAcademy. You could also try r/phphelp on reddit. Once you understand the language and its capabilities, you should easily be able to accomplish what you need.

If you have specific questions about the Twitch API, feel free to post those to get help.

@Dersarius like @DallasNChains is trying tell you, no one here just wants to hand out answers repeatedly. If you copy and paste code but never learn what’s it actually does you will continue to copy and paste forever, and never learn.

If you can understand what the original code you posted does… then you should be able to easily understand how to add a function and make it work.


// set the value $url to the api page/call you want to make
$url = ‘’;

// append the URL set above to the built-in function called “file_get_contents” and use this function to download that page/data
$json = file_get_contents($url);

// run(parse) the downloaded contents through the json_decoder to make it human readable, store it as an array called data
$data = json_decode($json, true);

// store the “bio” field from the array to the variable $bio
$bio = $data[‘bio’];

// echo the value of $bio
echo “bio: $bio”;


// Set the client ID for your app here
$clientID = array(
‘Client-ID: 0000000000000000000000000000000’

//Create a function called “file_get_contents_curl” - This will work/replace any usage of the normal “file_get_contents” and will include the required header with ClientID field.

function file_get_contents_curl($url) {
$ch = curl_init();

// this area is the curl options being set, to include the URL to be called and the ClientID to be added to the header
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $clientID);

$data = curl_exec($ch);
return $data;


// Let’s do a sample call, I want to see who my last follower is so I’ll call
// Storing the data as apiCALL
$apiCALL = json_decode(@file_get_contents_curl(‘’), true);
// Grabbing the field I want, and storing it’s value as ‘$follower’
$follower = $apiCALL[‘follows’][‘0’][‘user’][‘name’];
//echo the value for ‘$follower’
echo $follower;


@Dersarius I have put comments in, the comments are prefixed with //

With everything provided, even someone with no understanding of the API should be able to get it working with a little effort.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.