0

I'm trying to use an API which in JSON and interpret the data in PHP. I can output the all of the data using this below. But I can't seem to find a way to decode the data and then extract what I would like.

$url = "https://home-api.letmc.com/v2/tier3/.......";    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);    
        curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $output = curl_exec($ch);
        curl_close($ch);

        $data = $output;

This is the output of the API before using the above.

{
  "Data": [
    {
      "OID": "060a-06fd-1176-7964",
    }
  ],
  "Count": 1
}

Is there an easy way for me to using my PHP I have so far, just echo the OID from the API without everything else showing?

cn007b
  • 16,596
  • 7
  • 59
  • 74

1 Answers1

1
<?php

$json = '{
  "Data": [
    {
      "OID": "060a-06fd-1176-7964"
    }
  ],
  "Count": 1
}';
$data = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
    throw new RuntimeException('Invalid JSON.');
}
if (isset($data['Data'][0]['OID'])) {
    var_export($data['Data'][0]['OID']);
}
cn007b
  • 16,596
  • 7
  • 59
  • 74