我遇到这种情况: 我想使用curl向表单发出PUT请求,但它一直在检索此错误:"发送失败:连接中止"。这是我使用的功能:
function PutCurl($url,$parametros_post){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch,CURLOPT_HEADER,false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_COOKIEJAR,'cookies.txt');
curl_setopt($ch,CURLOPT_COOKIEFILE,'cookies.txt');
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch,CURLOPT_PUT,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: '.strlen($parametros_post)));
curl_setopt($ch,CURLOPT_POSTFIELDS,$parametros_post);
$result = curl_exec($ch);
$rerror = curl_error($ch);
curl_close($ch);
if ($rerror != ""){
echo '<h3>'.$rerror.'</h3>';
return false;
}
else
return $result;
}
我收到此错误:
尝试处理请求时:
PUT / textos-del-anuncio / HTTP / 1.0 User-Agent:Mozilla / 4.0(兼容; MSIE 5.01; Windows NT 5.0) 主持人:www.somesite.com Pragma:没有缓存 接受: / Cookie:PHPSESSID = gkmb7ksi1o82d91rino35ihma3 期待:100-继续 连接:关闭 X-Forwarded-For:Ip 通过:1.0代理+(v4.00 http://www.proxyplus.cz) 代理授权:基本a2F2OjNHd3BiK3J3QiE =
遇到以下错误:
* Invalid Request
HTTP请求的某些方面无效。可能的问题:
* Missing or unknown request method
* Missing URL
* Missing HTTP Identifier (HTTP/1.0)
* Request is too large
* Content-Length missing for POST or PUT requests
* Illegal character in hostname; underscores are not allowed
然后我尝试在标题中设置Content-Length,将此行添加到我的函数中:
curl_setopt($ ch,CURLOPT_HTTPHEADER,array(&#39; Content-Length:&#39; .strlen($ parametros_post)));
然后我得到一个&#34;发送失败:连接被中止&#34;错误。
拜托,有人可以帮助我。感谢。
答案 0 :(得分:0)
请求有问题,您是否尝试过这样做:
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
所以你不要使用curl_setopt($ ch,CURLOPT_PUT,true);但改为CURLOPT_CUSTOMREQUEST。 我假设您的$ parametros_post是一个关联数组,如$ parametros_post = array('post_field_name'=&gt;'post_field_value');