0

I'm very new to php (I know next to nothing, in fact) and I'm trying to display an object from a JSON string on a website (probably not the right terminology but you know... I'm new...). Here's the cURL code I'm using:

$url="http://mc.gl:8081/?JSONSERVER=lobby";
//  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);

Then I have this that's supposed to do something (I really dont understand it)

// Will dump a beauty json :3
var_dump(json_decode($result));

Annnnnddd.... what do I do next? I've done a lot of googling and nothing seems to work. Here's the string that I should be getting:

{"lobby":{"playeramount":1,"players":{"MisterErwin":"MisterErwin"}},"API-Version":"1","return":true}

and I want to echo "playeramount".

Any help would be greatly appreciated! Thanks so much!

1
  • Why you used CURL (is there any special reason), Have you tried ajax (using jquery) its more simple. check this http://www.lennu.net/2012/06/25/jquery-ajax-example-with-json-response/ Nov 8, 2013 at 15:55

2 Answers 2

2

The var_dump() function in PHP is used to display structured information about variables. It is usually used for debugging and has nothing to do with the JSON decode.

In this case, the $result variable will contain the JSON string you need. To decode it, use PHP's built-in function json_decode():

$json = json_decode($result); // decode JSON string into an object

Note: It's also possible to get an associative array by passing TRUE as the second parameter to json_decode().

Once you have the object, you can traverse it to get the required value:

echo $json->lobby->playeramount;

Demo!

2
  • Thank you for you swift response! However, it didn't work for me. Am I supposed to replace // Will dump a beauty json :3 var_dump(json_decode($result)); with $json = json_decode($result);?
    – Net
    Nov 8, 2013 at 15:55
  • 1
    For some reason, it's still not working. Obviously it works on codepad.viper-7.com so there shouldnt be anything wrong with your code. I'm using drupal and it returns this error: Notice: Trying to get property of non-object in include() (line 49 of /home/thenet/public_html/sites/all/modules/custompage/test1.tpl.php). Notice: Trying to get property of non-object in include() (line 49 of /home/thenet/public_html/sites/all/modules/custompage/test1.tpl.php). If you think you can help, I would really appreciate it. If not, thanks anyways!
    – Net
    Nov 8, 2013 at 16:09
1

If you want to access the result as an associative array, you can do like this too [By passing a true flag in the json_decode() function]

<?php
$str='{"lobby":{"playeramount":1,"players":{"MisterErwin":"MisterErwin"}},"API-Version":"1","return":true}';
$str=json_decode($str,true); // Setting the true flag 
echo $str['lobby']['playeramount']; //Outputs 1

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.