简体   繁体   中英

php Length Required error when trying to send request using curl soap

I need to form a request under this format:

    POST /CJW/cjws.asmx HTTP/1.1
Host: www.somesite.abc.edu.sg
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/GetAll"

<?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>
    <GetAll xmlns="http://tempuri.org/">
      <ErrorMsg>string</ErrorMsg>
    </GetAll>
  </soap:Body>
</soap:Envelope>

So here is what I tried:

<?php
$username = '';
$password = '';
$credentials = $username.":".$password; 
//$url = "http://tempuri.org/GetAll";
$url = "http://www.somesite.abc.edu.sg/GetAll";

$soap_request = '<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>
    <GetAll xmlns="http://tempuri.org/">
      <ErrorMsg>string</ErrorMsg>
    </GetAll>
  </soap:Body>
</soap:Envelope>';

$header = array(
"POST /CJW/cjws.asmx HTTP/1.1 \n",
"Host: www.somesite.abc.edu.sg \n",
"Content-type: text/xml; charset=\"utf-8\" \n",
"Content-length: ".strlen($soap_request)." \n",
"SOAPAction: ".$url." \n");


$curl = curl_init(); 


curl_setopt($curl, CURLOPT_PROXY, '127.0.0.1:8888'); 
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ; 
curl_setopt($curl, CURLOPT_USERPWD, $credentials); 
curl_setopt($curl, CURLOPT_SSLVERSION,3); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($curl, CURLOPT_HEADER, true); 
curl_setopt($curl, CURLOPT_POST, 1); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $soap_request );
curl_setopt($curl, CURLOPT_HTTPHEADER,     $header);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); 
curl_setopt($curl, CURLOPT_URL, $url);

// perform the request

$xml_result = curl_exec($curl);
// check for errors
if ($xml_result === false) {
$error_occurred = true;
}
else {

    echo $xml_result;
}
?>

But I cannot get the response correctly, actually, I do not see the response except this error:

Length Required
HTTP Error 411. The request must be chunked or have a content length.

I tried many way including remove the length part, change it with some fix number, etc. but it changes nothing. So what I am doing wrong here ? How is the right way ? Thanks a lot !

(You can remove Host: and Content-Length: from the headers you synthesize because curl will add them both itself if you don't set them.)

You should inspect your outgoing request in detail and verify that it looks exactly the way you want it to. Using CURLOPT_VERBOSE is usually enough but you need to be able to get that output somewhere for inspection.

I believe your custom headers all have a trailing space and a newline that you should remove. They will severely screw up the outgoing request. Oh, and the POST part must be removed from your custom headers (it is not a header, it is the "request-line").

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