json_decode need help

Well, I am trying to use json_decode to get a users twitch name via their steam ID, however I am getting an error, and I have read other users issues and I am no closer to fixing it.

Here is my code:

$getcontents = file_get_contents('http://api.twitch.tv/api/steam/76561198049928469'); var_dump(json_decode($getcontents)); $twitchname = $getcontents ['name']; echo $twitchname;

Here is my error:

Warning: Illegal string offset ‘name’ in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\Portfolio – Website\forum\index.php on line 29

json_decode doesn’t decode in-place (it doesn’t automatically rewrite the variable you pass in). You need to do $getcontents = json_decode($getcontents);

This decodes to a object so;

$getcontents = json_decode($getcontents);
$twitchname = $getcontents->name;

If you want an array

$getcontents = json_decode($getcontents, true);
$twitchname = $getcontents['name'];

http://php.net/json_decode

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