I am calling an external api endpoint and it returns an XML. I am trying to catch if the the translated json from the xml has a current array key since it does not have an status column so I manually trying to do some condition.
Here is what I tried
My code in transforming xml to json and printing the response
$parsed_xml = json_encode(simplexml_load_string($response));
$finalJson = json_decode($parsed_xml,true);
dd($finalJson);
if(isset($finalJson['ErrorResponse'])){
dd('Error response');
}else{
dd($finalJson['GetTransportContentResult']['TransportContent']['TransportHeader']['IsPartnered']);
}
When I run php artisan tinker and call the controller method (without the conditions for checking the response, i used dd($finalJson), here are the outputs
If the response has error, the translated xml becomes like this
array:2 [
"Error" => array:3 [
"Type" => "Sender"
"Code" => "AccessDenied"
"Message" => "AuthToken is not valid for SellerId and AWSAccountId."
]
"RequestID" => "be9742cd-516b-43f7-9a9e-bee00ad07b4f"
]
But if it is successful the response is
array:2 [
"GetTransportContentResult" => array:1 [
"TransportContent" => array:3 [
"TransportHeader" => array:4 [
"SellerId" => "AZZQLCSVQ753J"
"ShipmentId" => "FBA15XJRFN14"
"IsPartnered" => "true"
"ShipmentType" => "LTL"
]
I dont know if I add the if-else code i wrote, it returns true in tinker and it does not go to print the dd('Error response') I also tried using array_key_exists('Error',$finalJson) but it does the same thing. Can someone tell me if I am doing the correct thing or I missed something. Thanks a lot.
EDIT: @RigsFolly want me to show the raw result of $response, I just used dd($response) and run the method in tinker and these are the results
If the response is success
<GetTransportContentResponse xmlns="http://mws.amazonaws.com/FulfillmentInboundShipment/2010-10-01/">\n
<GetTransportContentResult>\n
<TransportContent>\n
<TransportHeader>\n
<SellerId>AZZQLCSVQ753J</SellerId>\n
<ShipmentId>FBA15XJRFN14</ShipmentId>\n
<IsPartnered>true</IsPartnered>\n
<ShipmentType>LTL</ShipmentType>\n
</TransportHeader>\n
<TransportDetails>\n
<PartneredLtlData>\n
<IsBillOfLadingAvailable>true</IsBillOfLadingAvailable>\n
<FreightReadyDate>Tue Jan 12 00:00:00 GMT 2021</FreightReadyDate>\n
<AmazonReferenceId>12113840411</AmazonReferenceId>\n
<PreviewPickupDate>Wed Jan 13 00:00:00 GMT 2021</PreviewPickupDate>\n
<PreviewFreightClass>175.0</PreviewFreightClass>\n
<PartneredEstimate>\n
<Amount>\n
<CurrencyCode>USD</CurrencyCode>\n
<Value>229.95</Value>\n
</Amount>\n
<VoidDeadline>2021-01-12T17:34:56Z</VoidDeadline>\n
</PartneredEstimate>\n
<SellerDeclaredValue>\n
<CurrencyCode>USD</CurrencyCode>\n
<Value>1006.40</Value>\n
</SellerDeclaredValue>\n
<PalletList>\n
<member>\n
<IsStacked>false</IsStacked>\n
<Weight>\n
<Unit>pounds</Unit>\n
<Value>344.0</Value>\n
</Weight>\n
<Dimensions>\n
<Unit>IN</Unit>\n
<Width>40.0</Width>\n
<Length>48.0</Length>\n
<Height>45.0</Height>\n
</Dimensions>\n
</member>\n
</PalletList>\n
<BoxCount>34</BoxCount>\n
<Contact>\n
<Phone>(510) 970-9910</Phone>\n
<Name>Mario</Name>\n
<Email>[email protected]</Email>\n
</Contact>\n
<TotalWeight>\n
<Unit>pounds</Unit>\n
<Value>344.0</Value>\n
</TotalWeight>\n
</PartneredLtlData>\n
</TransportDetails>\n
<TransportResult>\n
<TransportStatus>CONFIRMED</TransportStatus>\n
</TransportResult>\n
</TransportContent>\n
</GetTransportContentResult>\n
<ResponseMetadata>\n
<RequestId>313995bb-b198-469d-9c95-af4feb1a0745</RequestId>\n
</ResponseMetadata>\n
</GetTransportContentResponse>\n
If it has an error
<?xml version="1.0"?>\n
<ErrorResponse xmlns="http://mws.amazonaws.com/FulfillmentInboundShipment/2010-10-01/">\n
<Error>\n
<Type>Sender</Type>\n
<Code>AccessDenied</Code>\n
<Message>AuthToken is not valid for SellerId and AWSAccountId.</Message>\n
</Error>\n
<RequestID>b0df9b31-fe36-4936-883e-1c8b19157ba9</RequestID>\n
</ErrorResponse>\n
$response