Unwanted lines attached at the beginning and end of a SOAP response in PHP SoapClient

I am executing the following php code when trying to get a request from a SOAP API server

try { $soap = new SoapClient($wsdl, $options); $data = $soap->GetXYZ($params); } catch(Exception $e) { $Lastresponse = $soap->__getLastResponse(); } 

All I got was the code "it looks like we don’t have an XML document."

But when I looked at the $ Lastresponse variable in the catch block, I found it as below:

------=_Part_1134075_393252946.1482317378966 Content-Type: application/xop+xml; charset=utf-8; type="text/xml" <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header/> <SOAP-ENV:Body> ......all valid data ... </SOAP-ENV:Body> </SOAP-ENV:Envelope> ------=_Part_1134075_393252946.1482317378966--

Note. The options $ options I use are:

 $options = array( 'uri'=>'http://schemas.xmlsoap.org/soap/envelope/', //'style'=>SOAP_RPC, //'use'=>SOAP_ENCODED, 'soap_version'=>SOAP_1_1, 'cache_wsdl'=>WSDL_CACHE_NONE, 'connection_timeout'=>15, 'trace'=>true, 'encoding'=>'UTF-8', 'exceptions'=>true ); 

Although I made a workaround for xml parsing, does anyone have an idea about these extra bits parts? Is there something I'm doing wrong?

+8
soap xml php web-services
source share
3 answers

Those -----Part things are called the boundary in multicast messages, explained here and defined in RFC2387 .

After the investigation, SoapClient seems to be unable to parse multi-page messages, so you got this exception.

The solution is to extend SoapClient to extract XML content with regular expression or other string functions. Here is an example from this page :

 class MySoapClient extends SoapClient { public function __doRequest($request, $location, $action, $version, $one_way = 0) { $response = parent::__doRequest($request, $location, $action, $version, $one_way); $start = strpos($response,'<?xml'); $end = strrpos($response,'>'); return substr($response,$start,$end-$start+1); } } 
+5
source share

Poll request using the query below

  $headers = array( "POST: ".url." HTTP/1.1", "Host: ".domain."", "Accept-Encoding: gzip,deflate", "Content-type: text/xml;charset=UTF-8", "SOAPAction: \"http://tempuri.org/wsdlurl/methodname\"", "Connection: Keep-Alive", "Content-length:".strlen($xml), "User-Agent: Apache-HttpClient/4.1.1 (java 1.5) " ) $url = your url; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_ENCODING, ''); curl_setopt($ch, CURLOPT_POSTFIELDS, $options); // the SOAP request curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); $err = curl_error($ch); $doc = new DOMDocument(); $doc->loadXML( $response ); $authKey = $doc->getElementsByTagName( "fieldname" ); 
+2
source share

You must catch the exception:

SoapClient :: SoapClient () generates an E_ERROR error if location and uri parameters are not provided in a mode other than WSDL.

A SoapFault exception will be thrown if the wsdl URI cannot be loaded.

+2
source share

All Articles