0

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
2
  • Can you show us the raw $response Commented Feb 28, 2021 at 13:29
  • you mean the xml sir? @RiggsFolly Commented Feb 28, 2021 at 13:30

1 Answer 1

1

The xmlns is a definitions of the document not part of the documents contents.

So you are testing the wrong part of the XML, instead try

if(isset($finalJson['Error'])){
    echo 'Error response';
}else{
    echo $finalJson['GetTransportContentResult']['TransportContent']['TransportHeader']['IsPartnered'];
}
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for sharing your answer sir. It kinda works when the result has an error but when it doesnt have an error. It also returns true and not going to the else part. anything i missed?
Can you show ALL of a good response then please, what you showed is not even valid XML
okay sir im sorry. I edited the question pasting the whole results for a response without error
Again, not valid xml
sorry sir i was forgotten i am getting the column IsPartnered and it only returns true or false. I was confused becaseu of the method before that returns true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.