0

I am trying to get an api(steam api) response as a variable.

{"response":{"players":[{"steamid":"XXXXXXXXXXXXXXXXXXXX","communityvisibilitystate":3,"profilestate":1,"personaname":"The value I want to get","lastlogoff":XXXXXXXXXX,"profileurl":"XXX","avatar":"X","avatarmedium":"X","avatarfull":"X","personastate":1,"realname":"X","primaryclanid":"X","timecreated":XXXXXXXXXX,"personastateflags":0}]}}

Formatted: https://pastebin.com/8QtLzwVX

Currently I am using the following code to get the array:

$url="http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&steamids=" . $sid;
//  Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);

I don't know how to get the personaname value from the JSON I get in $result and put it in a variable like $personaname.

1 Answers1

0

try with this one:

<?php

$json_array = json_decode($result, true);
echo $json_array['response']['players'][0]['personaname'];
  • It's not echoing anything. – ImRonny Dark Oct 24 '18 at 18:30
  • Let me check it online – Aleksandar Rakic Oct 24 '18 at 18:31
  • According to my test your JSON array is not valid, all fields must be with double quotes, except numbers and decimal numbers. So you need to fix two lines timecreated, lastlogoff. When you fix that everything else should work fine. Ofc you need to adjust variables to your code :-) – Aleksandar Rakic Oct 24 '18 at 18:35
  • I know, in the real API response the "x"s are numbers. I just replaced them for privacy. However I got it working by using this: `print_r($array->response->players[0]->personaname);` where `$array = json_decode($result);` – ImRonny Dark Oct 24 '18 at 21:12
  • Dude its same thing, you just got object instead of array. When you don't put true as a second argument in json_decode it will return object and you need to access it like you are, otherwise it will return an array, like solution above. PHP Manual : When TRUE, returned objects will be converted into associative arrays. – Aleksandar Rakic Oct 25 '18 at 06:31