0

XML response want to convert into PHP array it works fine in $response but when I convert into an array it will become empty. How can I convert this XML into an array to display it on the web page? Any information that would point me in the correct direction would be great.

$soapUrl = "http://api.rlcarriers.com/1.0.2/RateQuoteService.asmx"; // asmx URL of WSDL
    $xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <GetRateQuote xmlns="http://www.rlcarriers.com/">
          <APIKey>UtN0YWEGRhOjNiNmItODRkMS00NzgyLThiYzNTViEzNjODNmC</APIKey>
          <request>
            <QuoteType>Domestic</QuoteType>
            <CODAmount>0</CODAmount>
            <Origin>
              <City>New York</City>
              <StateOrProvince>NY</StateOrProvince>
              <ZipOrPostalCode>10001</ZipOrPostalCode>
              <CountryCode>USA</CountryCode>
            </Origin>
            <Destination>
              <City>New York</City>
              <StateOrProvince>NY</StateOrProvince>
              <ZipOrPostalCode>10009</ZipOrPostalCode>
              <CountryCode>USA</CountryCode>
            </Destination>
            <Items>
              <Item>
                <Class>150.0</Class>
                <Weight>2.5</Weight>
                <Width>2.5</Width>
                <Height>2.5</Height>
                <Length>2.1</Length>
              </Item>

            </Items>
            <DeclaredValue>120</DeclaredValue>
            <Accessorials>
              <Accessorial>ResidentialPickup</Accessorial>
              <Accessorial>ResidentialDelivery</Accessorial>
            </Accessorials>
            <OverDimensionPcs>0</OverDimensionPcs>

          </request>
        </GetRateQuote>
      </soap:Body>
    </soap:Envelope>';  

        $headers = array(
        "Content-type: text/xml;charset=\"utf-8\"",
        "Accept: text/xml",
        "Cache-Control: no-cache",
        "Pragma: no-cache",
        "SOAPAction: http://www.rlcarriers.com/GetRateQuote", 
        "Content-length: ".strlen($xml_post_string),
        ); //SOAPAction: your op URL

        $url = $soapUrl;

        // PHP cURL  for https connection with auth
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        // converting
        $response = curl_exec($ch); 
        curl_close($ch);
        var_dump($response);exit;

        $xml = simplexml_load_string($response);
        $json = json_encode($xml);
        $array = json_decode($json,TRUE);
        print_r($array);

Below is the response received from the above code.

string(4037) "trueNEW YORKNY10001USANEW YORKNY10009USALISLong Island, NY113781-888-575-2632MASPETHNYLISLong Island, NY113781-888-575-2632MASPETHNY$420.352$442.90$8.86MINIMUM$510.35DISCNTFloor$420.35DISCNF$90.00FUEL23.1%$20.79ARBMH$65.00RPUCWT$132.30RCCWT$132.30NET$440.39STD71825391$839.95$440.39GSDS297780121$38.90$479.29GSAM382622281$77.70$518.09"

A R
  • 1,412
  • 2
  • 14
  • 29
  • 1
    Is that the response from `var_dump($response);`? Are you certain you are getting XML from the response? – McRed Sep 19 '17 at 16:14

1 Answers1

0

Those are SOAP XML, change it's heading by

    ...
    // converting
    $response = curl_exec($ch); 
    curl_close($ch);

    $clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $response);

    $xml = simplexml_load_string($clean_xml);
    $json = json_encode($xml);
    $array = json_decode($json,TRUE);
    print_r($array);

see: How to parse SOAP XML?

Ryan Harne
  • 469
  • 3
  • 7