Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

monstajamss's avatar

Is there a way to get value of json object from a URL in PHP?

So i am trying to get values of json object from a url, when i hit that url using the GET method on postman i get something like this

{
    "error": "0",
    "errorString": "",
    "reply": {
        "nonce": "5e415334832a8",
        "realm": "VMS"
    }
}

So, i am trying to write a php code that displays the value of nonce in the browser but it is not working i have the following code

$getNonceUrl = "https://example.com/api/getNonce";
$getContect = file_get_contents($getNonceUrl);
$jsonNoce = json_decode($getContect, true);
$dd = $jsonNoce->reply[0]->nonce;
echo $dd;

I also did this

$ch = curl_init("https://193.178.55.230:3389/api/getNonce");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
echo $ch->reply[0]->nonce;

But it does not seem to work still.

0 likes
9 replies
monstajamss's avatar

@sr57 the thing is that i want to get it from the link using the GET Method

sr57's avatar

@monstajamss

Of course, but your first lines should work.

$getNonceUrl = "https://example.com/api/getNonce";
$getContect = file_get_contents($getNonceUrl);
monstajamss's avatar

@sr57 i get this boolean false when i

$getNonceUrl = "https://193.178.55.230:3389/api/getNonce";

$getContect = file_get_contents($getNonceUrl);
var_dump($getContect);
monstajamss's avatar

@sr57 Oh yeah you are right, when i changed https to http it worked :)

below is the full solution

$getNonceUrl = "http://example.com/api/getNonce";
$getContect = file_get_contents($getNonceUrl);
$jsonNoce = json_decode($getContect, true);
$dd = $jsonNoce['reply']['nonce'];
echo $dd;
exit();
psrz's avatar

I imagine the 2nd bit is the php code generated by Postman

If there was no error, $data should be a json string so this should work:

$array = json_decode($data, true);
var_export($array);
psrz's avatar

@monstajamss If var_export() is showing null then $data is an invalid json string

After curl_exec() add these lines

$error = curl_error($ch);
curl_close($ch);

if ($error) {
  echo "cURL Error #:" . $error;
} else {
  echo $data;
}
2 likes

Please or to participate in this conversation.