【问题标题】:Connection aborted when posting with curl使用 curl 发布时连接中止
【发布时间】:2014-02-12 18:02:18
【问题描述】:

我有这种情况: 我想使用 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 用户代理: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('Content-Length: '.strlen($parametros_post)));

但随后我收到“发送失败:连接已中止”错误。

拜托,谁能帮助我。谢谢。

【问题讨论】:

  • 那不是你真正的代理用户和密码吗?!
  • 您也可以发布 curl 错误代码吗?
  • 我在帖子中添加了错误描述。你能帮我吗?

标签: php curl put


【解决方案1】:

请求有问题,您是否尝试过这样做:

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' => 'post_field_value');

【讨论】:

  • 嗯,不。我的 $parametros_post 是我这样构建的字符串: 'field_name='.urlencode($field_value);这在我所做的每一个 POST 中都对我有用,但现在方法是 PUT。
  • 当我使用 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");它没有用。我收到了来自网站的回复,告诉我我错过了字段的值。
  • 您是否尝试过将 $parametros_post 作为关联数组同时使用 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");和 curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));就像我上面的例子一样?
  • 是的,我按照你说的方式尝试过了,但我一直收到同样的 Invalid Request 错误。
猜你喜欢
  • 2021-09-04
  • 2011-03-31
  • 1970-01-01
  • 1970-01-01
  • 2014-09-15
  • 1970-01-01
  • 1970-01-01
  • 2014-01-22
  • 2015-02-16
相关资源
最近更新 更多