hopefully someone can help.
The script below is a simple PHP and Curl script and basically when I echo the response I get the information back but obviously before I encode it for Json. The below is my PHP and the $response is working fine, however I cannot get an output from $reponse2
My issue is i want to echo the lower
$response = curl_exec($curl);
echo $response;
$ob = simplexml_load_string($response);
$json = json_encode($ob);
$response2 = json_decode($json, true);
echo $response2;
$TRACKINGData = $response2['trackingdetails'];
for($i=0;$i<count($TRACKINGData);$i ) {
echo 'Tracking details: ' .$TRACKINGData[$i]['trackingnumber'].'';
} ?>
for($i=0;$i<count($TRACKINGData);$i ) {
echo 'Tracking details: ' .$TRACKINGData[$i]['trackingnumber'].'';
} ?>
Please see the response i get from Postman `
<trackingresponse>
<trackingdetails>
<trackingdetail>
<trackingnumber>4569788591</trackingnumber>
<parcelnumber>15504569788591</parcelnumber>
<consignmentnumber>4569788591</consignmentnumber>
<deliveryaddress1></deliveryaddress1>
<deliveryaddress2>Bramble Wood</deliveryaddress2>
<deliverylocality>1Street</deliverylocality>
<deliverycity></deliverycity>
<deliverypostcode></deliverypostcode>
<deliverycountrycode>GB</deliverycountrycode>
<etastart>1970-01-01T12:31:00 01:00</etastart>
<etaend>1970-01-01T13:31:00 01:00</etaend>
<parcelcode>15504569788591*19990</parcelcode>
<trackingevents>
<trackingevent>
<date>2022-09-24T08:32:00 01:00</date>
<code>15</code>
<type>OUT FOR DELIVERY</type>
<description>On vehicle for delivery between 12:31 - 13:31 Out For Delivery SMS notification sent</description>
<locality>Peterborough</locality>
<countrycode>GB</countrycode>
<etastart>12:31</etastart>
<etaend>13:31</etaend>
<imagekey></imagekey>
</trackingevent>
<trackingevent>
<date>2022-09-24T00:36:00 01:00</date>
<code>4</code>
<type>CONFIRMED AT DEPOT</type>
<description>Confirmed at depot</description>
<locality>Peterborough</locality>
<countrycode>GB</countrycode>
<imagekey></imagekey>
</trackingevent>
<trackingevent>
<date>2022-09-24T00:17:00 01:00</date>
<code>4</code>
<type>CONFIRMED AT DEPOT</type>
<description>Confirmed at depot</description>
<locality>Peterborough</locality>
<countrycode>GB</countrycode>
<imagekey></imagekey>
</trackingevent>
<trackingevent>
<date>2022-09-23T20:01:00 01:00</date>
<code>9</code>
<type>HUB SCAN</type>
<description>Confirmed at Hub</description>
<locality>Hub 3 - Bham</locality>
<countrycode>GB</countrycode>
<imagekey></imagekey>
</trackingevent>
<trackingevent>
<date>2022-09-23T18:34:00 01:00</date>
<code>0</code>
<type>PARCEL DATA</type>
<description>Customer data received</description>
<locality>Birmingham</locality>
<countrycode>GB</countrycode>
<imagekey></imagekey>
</trackingevent>
</trackingevents>
</trackingdetail>
</trackingdetails>
</trackingresponse>`
CodePudding user response:
$response2 is an array, not a string. Convert $response2 to a string before the echo.
echo var_export($response2,true);
CodePudding user response:
I wouldn’t bother with the JSON, it can actually mess with you if you ever have complex XML. Instead, SimpleXML makes it really easy to access nodes or iterate over them:
$obj = simplexml_load_string($xml);
// if you actually think you will get multiple
foreach($obj->trackingdetails->trackingdetail as $trackingDetail){
echo $trackingDetail->trackingnumber;
}
echo PHP_EOL;
// if you know you'll always have just one
echo $obj->trackingdetails->trackingdetail->trackingnumber;
Demo: https://3v4l.org/THok5