【问题标题】:php Length Required error when trying to send request using curl soap尝试使用 curl soap 发送请求时出现 php Length Required 错误
【发布时间】:2014-08-04 06:02:54
【问题描述】:

我需要在这种格式下形成一个请求:

    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>

所以这是我尝试过的:

<?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;
}
?>

但我无法正确得到响应,实际上,除了这个错误,我没有看到响应:

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

我尝试了很多方法,包括删除长度部分,用一些修复号更改它等等,但它没有任何改变。 那么我在这里做错了什么?正确的方法是怎样的? 非常感谢!

【问题讨论】:

    标签: php curl soap libcurl


    【解决方案1】:

    (您可以从您合成的标头中删除 Host: 和 Content-Length:,因为如果您不设置它们,curl 会自行添加它们。)

    您应该详细检查您发出的请求,并确认它看起来完全符合您的要求。使用 CURLOPT_VERBOSE 通常就足够了,但您需要能够在某处获得该输出以进行检查。

    我相信您的自定义标题都有一个尾随空格和一个应该删除的换行符。他们将严重搞砸发出的请求。哦,POST 部分必须从您的自定义标题中删除(它不是标题,它是“请求行”)。

    【讨论】:

    • 我删除了空格和换行冗余,删除了主机:和内容长度;将 $soap_request 缩短为一行。它不显示错误,但会加载整个网站,而不是给出结果。我使用提琴手,它显示“罕见的例外,服务器必须包含日期响应标头”。我不知道那是什么。当我在调试窗口中查看“RAW”时,<...>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 2019-05-29
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多