【问题标题】:XML RESPONSE CONVERT INTO ARRAYXML 响应转换为数组
【发布时间】:2017-09-19 16:08:50
【问题描述】:

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 YORKNY10009USALIS长岛, NY113781-888-575-2632MASPETHNYLIS长岛, 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"

【问题讨论】:

  • 这是var_dump($response);的回复吗?您确定您从响应中获取 XML 吗?

标签: php arrays json xml codeigniter


【解决方案1】:

这些是 SOAP XML,将其标题更改为

... // 转换 $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?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 2021-11-02
    • 1970-01-01
    相关资源
    最近更新 更多