简体   繁体   中英

Bad Request on PHP Curl request to SOAP web service

Here is the method I'm using:

$xml_data = 'XML data';

$headers = array(
"POST /ws_autos.asmx HTTP/1.1",
"Host: (web service url)",
"Content-Type: text/xml; charset=utf-8",
"SOAPAction: \"http://tempuri.org/(web service method)\"",
"Content-length: ".strlen($xml_data)
);

$url = (web service url)?wsdl;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);

$reply = curl_exec($ch); 



$xmli = new SimpleXMLElement($reply);


print_r($reply);

print_r($xmli);

I'm getting a Bad Request error without clarification upon excecution. I've checked the XML body for mistakes but there seem to be none, and there's nothing out of place in the header as far as I know.

I've tried using SoapClient but I can't find any documentation/resources on building the XML body with the amounts of nesting going on here.

your xml is corrupted, STRICTLY speaking.

DOMDocument::loadXML is a very strict XML parser, and it says this: Warning: DOMDocument::loadXML(): XML declaration allowed only at the start of the document in Entity, line: 2 in /in/EOrYe on line 43

because: 换行符破坏了您的XML

your <?xml declaration is not the first thing in your XML. the very first thing in your XML is a newline, THEN comes your <?xml . you can either remove the newline, or you can replace

curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);

with

curl_setopt($ch, CURLOPT_POSTFIELDS, trim($xml_data));

also get rid of

"Content-length: ".strlen($xml_data)

because libcurl will automatically add it for you if you don't, and libcurl won't make any mistakes about it (unlike you, if you do curl_setopt($ch, CURLOPT_POSTFIELDS, trim($xml_data)); then the length will change because of the trim, so pretty much always let curl do it for you.)

also get rid of "POST /ws_autos.asmx HTTP/1.1"

because 1: curl will add this header for you. and 2: you don't know here if curl is going to use HTTP/1.0 or HTTP/1.1 or HTTP/2 , if curl decides to use HTTP/2 here, you'll corrupt the request.. (and curl DOES support HTTP/2 now, even tho it requires nginx libraries iirc and many servers' curl builds don't yet support it, but you wouldn't want your script to magically stop working the moment your curl build supports HTTP/2, would you? no, you wouldn't.)

also get rid of "Host: demo.caledoniaseguros.com.ar",

because curl will add it for you as long as the url is correct. pretty much the ONLY time you need (and should) set the Host: -header manually is when you have DNS issues, you can't trust your DNS, or the DNS lookup time is so slow that you can't take the performance hit, all those scenarios are exceedingly rare, and most often it's just a rookie mistake.

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