I'm having troubles getting a JSON object (possibly an array?) to output using PHP. My question is: How do I get only a displayName from the following (shortened) JSON object using PHP?
{"Response":
{"results":
[{"user":
{"membershipId":"6343960","displayName":"J Raider"},"hasPendingApplication":false},
{"user":
{"membershipId":"4479502","displayName":"T Ellis"},"hasPendingApplication":false}]
}
}
What I've tried so far:
var_dump($json);
echo $json;
echo $json->Response->results->user->displayName;
echo $json->Response->results[0]->user->displayName;
echo $json->Response->results[0]->user[0]->displayName;
echo json_encode($json, JSON_PRETTY_PRINT);
print_r($json)
My results have either been "null" "NULL" or simply no output. I am pulling from the Bungie API using the following code:
<?php
$apiKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.bungie.net/Platform/Group/1179713/Members/?lc=en&fmt=true¤tPage=1&platformType=1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-API-Key: ' . $apiKey));
$json = json_decode(curl_exec($ch)); // edited to correct curl_init to curl_exec
var_dump($json);
?>
I am aiming to do this as a web service using PHP. The Bungie API documentation says a JavaScript GET request will not work, so I'd like to stick with PHP for the request and the output.
To clarify my question: how do I use PHP to output the displayName from this JSON object?
Thanks in advance for your help and please let me know if I need to clarify anything.