mbenjemaa
mbenjemaa

Reputation: 79

CakePHP: getting a value from remote JSON file

I'm trying to get a specific value (the first value of 'EndDate') from a remote JSON file.

My JSON looks like this:

<AssetEntitlementData>
<AssetEntitlement>
    <EndDate>2016-03-18T23:59:59</EndDate>
    <EntitlementType>EXTENDED</EntitlementType>
    <ItemNumber>995-1933</ItemNumber>
    <ServiceLevelCode>ND</ServiceLevelCode>
</AssetEntitlement>
<AssetEntitlement>
    <EndDate>2016-03-18T23:59:59</EndDate>
    <EntitlementType>EXTENDED</EntitlementType>
    <ItemNumber>995-2093</ItemNumber>
</AssetEntitlement>
<AssetEntitlement>
    <EndDate>2014-03-18T23:59:59</EndDate>
    <EntitlementType>INITIAL</EntitlementType>
    <ItemNumber>995-0923</ItemNumber>
    <ServiceLevelCode>ND</ServiceLevelCode>
</AssetEntitlement>
<AssetEntitlement>
    <EndDate>2014-03-18T23:59:59</EndDate>
    <EntitlementType>INITIAL</EntitlementType>
    <ItemNumber>995-1553</ItemNumber>
    <ServiceLevelCode>TS</ServiceLevelCode>
</AssetEntitlement>
</AssetEntitlementData>
</AssetEntitlementData>

This is my code:

<?php 
     $tag = '11111111';
     $apikey='XXXXXXXXXXXXX';
     $url= 'https://example.com';
     $data = array('ID'=> $tag);
     $format = 'json';
     $options = array(
          'http' => array(
          'header'  => "Content-type: application/x-www-form-urlencoded\r\n"
               ."apikey : 5c1e4f40-8094-4719-9995-7475572d2efa\r\n"
               . "Accept : application/$format",
               'method'  => 'POST',
               'content' => http_build_query($data),
               )
          );
          $context  = stream_context_create($options);
          $result = file_get_contents($url1, false, $context);
          if ($result === FALSE) { /* Handle error */ 
                    }

          var_dump($result);
          if($format == 'json')
          {
              $response = json_decode($result);
          }

          var_dump($result->EndDate);
         echo "END";
?>

Trying:

var_dump($response->EndDate);

I got: Notice (8): Undefined property: stdClass::$EndDate

Trying:

var_dump($response['EndDate']);

I got an error:

Cannot use object of type stdClass as array.

enter image description here

The output for: die($result);

enter image description here

Upvotes: 0

Views: 403

Answers (1)

ubuntux
ubuntux

Reputation: 404

As what @Metalik said, your response data is not JSON, it is XML. If you want to access your data as XML object, use simplexml_load_string():

$xml = simplexml_load_string($result);

And to get the first value of EndDate:

var_dump((string)$xml->AssetEntitlement[0]->EndDate);

To access your data as JSON object, convert the $xml to JSON string then to JSON object:

$json = json_encode($xml); // To JSON string
$response = json_decode($json); // To JSON object

...to get the first value of EndDate from the JSON object:

var_dump($response->AssetEntitlement[0]->EndDate);

To access your data as associative array, convert the $xml to JSON string then to associative array:

$json = json_encode($xml); // To JSON string
$response = json_decode($json, true); // To associative array

...to get the first value of EndDate from the array:

var_dump($response['AssetEntitlement'][0]['EndDate']);

Lastly, your response data seemed to be missing an end tag for AssetEntitlementData. It should be:

<AssetEntitlementData>
    <AssetEntitlement>
        <EndDate>2016-03-18T23:59:59</EndDate>
        <EntitlementType>EXTENDED</EntitlementType>
        <ItemNumber>995-1933</ItemNumber>
        <ServiceLevelCode>ND</ServiceLevelCode>
    </AssetEntitlement>
    <AssetEntitlement>
        <EndDate>2016-03-18T23:59:59</EndDate>
        <EntitlementType>EXTENDED</EntitlementType>
        <ItemNumber>995-2093</ItemNumber>
    </AssetEntitlement>
    <AssetEntitlement>
        <EndDate>2014-03-18T23:59:59</EndDate>
        <EntitlementType>INITIAL</EntitlementType>
        <ItemNumber>995-0923</ItemNumber>
        <ServiceLevelCode>ND</ServiceLevelCode>
    </AssetEntitlement>
    <AssetEntitlement>
        <EndDate>2014-03-18T23:59:59</EndDate>
        <EntitlementType>INITIAL</EntitlementType>
        <ItemNumber>995-1553</ItemNumber>
        <ServiceLevelCode>TS</ServiceLevelCode>
    </AssetEntitlement>
</AssetEntitlementData>

Upvotes: 1

Related Questions