0

I am posting a GET cURL request to a server of which returns a JSON in the following string format (before running it through json_decode):

string(452)
"{
   "id":{
      "entityType":"DEVICE",
      "id":"abcdegfhijklmnop"
   },
   "createdTime":1668483881397,
   "additionalInfo":{
      "description":"SiteA"
   },
   "tenantId":{
      "entityType":"TENANT",
      "id":"123456789"
   },
   "customerId":{
      "entityType":"CUSTOMER",
      "id":"zyxwvu"
   },
   "name":"DeviceA",
   "type":"Measuring",
   "label":"DeviceA_123",
   "ownerId":{
      "entityType":"TENANT",
      "id":"123456789"
   }
}"

I am trying to access the data within ownerID->id and retrieve that value with the following code: print_r($result["ownerId"]["id"])

however when I run my script, all that is being returned is a single char: {.

I am posting a curl request with the following code:

$apiQuery = "https://api.server.com/api/tenant/devices";
$apiQuery .= "?";
$apiQuery .= http_build_query([
                'deviceName' => DeviceA,
            ]);

$headers = array(
                'Accept: application/json',
                'Content-Type: application/json',
                'charset=utf-8',
            );

$c = curl_init();
curl_setopt($c, CURLOPT_URL, $apiQuery);
curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
curl_setopt($c, CURLOPT_HEADER, 0);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($c, CURLOPT_TIMEOUT, 50000); // 50 sec

$result = curl_exec($c);

var_dump(json_decode($result));
print_r($result["ownerId"]["id"]);
jabroni
  • 167
  • 16
  • You're only dumping the result of `json_decode()`. You should instead assign that to a variable... `$data = json_decode($result, true); echo $data['ownerId']['id'];` – Phil Nov 22 '22 at 03:24
  • FYI your GET request has no _body_ so does not need a `content-type` header – Phil Nov 22 '22 at 03:26

0 Answers0