2017-09-19 8 views
0

XMLの応答は$応答でうまく動作しますが、配列に変換すると空になります。このXMLを配列に変換してWebページに表示するにはどうすればよいですか?正しい方向で私を指し示すどんな情報も素晴らしいでしょう。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); 

以下は上記のコードからの応答です。

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

+1

これはvar_dump($ response);の応答ですか?あなたは応答からXMLを得ていると確信していますか? – McRed

答えて

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); 
によって見出しています変更します0

参照:How to parse SOAP XML?