XML响应转换为ARRAY

时间:2017-09-19 16:08:51

标签: php arrays json xml codeigniter

XML响应希望转换为PHP数组,它在$ response中工作正常,但是当我转换为数组时,它将变为空。如何将此XML转换为数组以在网页上显示?任何能指明我正确方向的信息都会很棒。

$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);

以下是从上述代码收到的回复。

  

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"

1 个答案:

答案 0 :(得分:0)

这些是SOAP XML,改变它的标题

    ...
    // 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);

请参阅:How to parse SOAP XML?