简体   繁体   中英

Parsing response from the WSDL using PHP

I'm very sorry if I made a wrong title, I'm not familiar with SOAP response and types of it. But I guess it's a WSDL response, at least I got it from WSDL link...

I have a following url http://somedomain.com/j.svc?wsdl

And after I made a request using curl_multi I got the following response. The response was shortened to two results so it would be easier to read

The response is as following:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <GetJourneyListResponse xmlns="http://tempuri.org/">
          <GetJourneyListResult xmlns:a="http://schemas.datacontract.org/2004/07/DreamFlightWCF" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
              <a:Journey>
                 <a:FromAirport>LHR</a:FromAirport>
                 <a:TotalPrice>146</a:TotalPrice>
              </a:Journey>
              <a:Journey>
                 <a:FromAirport>LHR</a:FromAirport>
                 <a:TotalPrice>155</a:TotalPrice>
              </a:Journey>
           </GetJourneyListResult>
      </GetJourneyListResponse>
  </s:Body>
</s:Envelope>

Is there any chance to parse the result using PHP? I made lots of searches including StackOverflow and here what I managed to find.

To parse the above response I can use following code:

$xml = simplexml_load_string($result);
$xml->registerXPathNamespace('flight','http://schemas.datacontract.org/2004/07/DreamFlightWCF');
   foreach ($xml->xpath('//flight:Journey') as $item){
     print_r($item);
   }

It seems that the above PHP code piece is correct by partially. I get the correct amount of "Journey"s but the $item by its own is empty.

Any solutions? Please don't advise to use SoapClient to retrieve the result. I can't move from curl_multi. I already have the result and I need to parse it. Thank you in advance

$soap_request  = "<?xml version=\"1.0\"?>\n";
      $soap_request .= "<soap:Envelope xmlns:soap=\"http://www.w3.org/2001/12/soap-envelope\" soap:encodingStyle=\"http://www.w3.org/2001/12/soap-encoding\">\n";
      $soap_request .= "  <soap:Body xmlns:m=\"http://www.example.org/stock\">\n";
      $soap_request .= "    <m:GetStockPrice>\n";
      $soap_request .= "      <m:StockName>IBM</m:StockName>\n";
      $soap_request .= "    </m:GetStockPrice>\n";
      $soap_request .= "  </soap:Body>\n";
      $soap_request .= "</soap:Envelope>";

      $header = array(
        "Content-type: text/xml;charset=\"utf-8\"",
        "Accept: text/xml",
        "Cache-Control: no-cache",
        "Pragma: no-cache",
        "SOAPAction: \"run\"",
        "Content-length: ".strlen($soap_request),
      );

      $soap_do = curl_init();
      curl_setopt($soap_do, CURLOPT_URL, "http://ecc6unitst.kaisa.com:8000/sap/bc/srt/wsdl/bndg_386D2B5BD851F337E1000000AC1264E4/wsdl11/allinone/standard/document?sap-client=400" );
      curl_setopt($soap_do, CURLOPT_USERPWD, "EBALOBORRP:welcome1");
      curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10);
      curl_setopt($soap_do, CURLOPT_TIMEOUT,        10);
      curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
      curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
      curl_setopt($soap_do, CURLOPT_POST,           true );
      curl_setopt($soap_do, CURLOPT_POSTFIELDS,     $soap_request);
      curl_setopt($soap_do, CURLOPT_HTTPHEADER,     $header);
      $str = curl_exec($soap_do);
      if(curl_exec($soap_do) === false) {
        $err = 'Curl error: ' . curl_error($soap_do);
        curl_close($soap_do);
        print $err;
      } else {
        curl_close($soap_do);
        var_dump($str);
        print 'Operation completed without any errors';
      }

首先尝试解析 SOAP 响应,然后尝试Google

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM