php cURL - 请求必须分块或具有内容长度

时间:2011-10-14 20:59:52

标签: php curl

今天当我遇到这个奇怪的错误时,我正在尝试使用'lib cURL',我找不到解决方法,你能帮我解决一下吗?

<?php
set_time_limit(0);
  class cURL
  {
  var $content;
  var $cookie;
  var $headers = array();
  var $ch;
  var $data;
  var $info;
  var $httpcode;
  var $proxy_ip;
  var $proxy_port;
  var $cookie_f;

  function __construct($cookie = '')
  {
      if (!empty($cookie)) {
          $this->cookie_f = $cookie;
      }
  }

  function setproxy($proxy)
  {
      list($ip, $porta) = explode(":", $proxy);
      $this->proxy_ip = $ip;
      $this->proxy_port = $porta;
  }

  function setcookie($cookie)
  {
      $this->cookie = $cookie;
  }

  function open($type, $url)
  {
      $this->ch = curl_init();
      curl_setopt($this->ch, CURLOPT_URL, $url);      
      curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
      curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($this->ch, CURLOPT_HEADER, true);
      //curl_setopt($this->ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded", "Content-length: 216"));
      curl_setopt($this->ch, CURLOPT_ENCODING, "");
      curl_setopt($this->ch, CURLOPT_COOKIEFILE, getcwd() . '/cookies.cookie');
      curl_setopt($this->ch, CURLOPT_COOKIEJAR, getcwd() . '/cookies.cookie');
      curl_setopt($this->ch, CURLOPT_USERAGENT, "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; pt-BR; rv:1.9.2.18) Gecko/20110614 Firefox/3.6.18");

      if (strtolower($type) == 'post') {
          curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, "POST");
          curl_setopt($this->ch, CURLOPT_POST, 1);
          curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->data);
          curl_setopt($this->ch, CURLOPT_HEADER, true);
      }

      $this->content = curl_exec($this->ch);
      curl_close($this->ch);
      return $this->content;

  }
  }

?>
<?php
    $curll = New cURL;
    $curll ->setCookie = "cookies.cookie";
    $curll ->data = "username=user&password=pass&IsSyncUp=&FacebookAssociation=&SNAccessToken=";
    $curll ->open("POST", "http://www.website.com");
    $curll ->data = '__EVENTTARGET=ctl00$ctl00$cphRoblox$cphMyRobloxContent$lbSend&ctl00$ctl00$cphRoblox$cphMyRobloxContent$rbxMessageEditor$txtSubject=AAAAAAAAA&ctl00$ctl00$cphRoblox$cphMyRobloxContent$rbxMessageEditor$txtBody=BBBBBBBBB';
    $curll ->open("POST", "http://www.website.com/My/PrivateMessage.aspx?RecipientID=20815603");
    echo $curll ->content;
?>

所以,我的返回错误是:

  

HTTP / 1.1 302 Found Cache-Control:private Content-Type:text / html; charset = utf-8位置:/Login/Default.aspx?ReturnUrl=%2fMy%2fPrivateMessage.aspx%3fRecipientID%3d20815603&RecipientID=20815603服务器:Microsoft-IIS / 7.0 Set-Cookie:rbx-ip =;路径= /; HttpOnly Set-Cookie:RBXSource = rbx_acquisition_time = 10/14/2011 3:32:12 PM&amp; rbx_acquisition_referrer = http://www.website.com/My/PrivateMessage.aspx?RecipientID = 20815603&amp; rbx_medium = Direct&amp; rbx_source = www .website.com&安培; rbx_campaign =安培; rbx_send_info = 0; expires = Sun,13-Nov-2011 21:32:12 GMT; path = / X-AspNet-Version:4.0.30319 X-Powered-By:ASP.NET P3P:CP =“CAO DSP COR CURA ADMa DEVa我们的IND PHY ONL UNI COM NAV INT DEM PRE”日期:2011年10月14日星期五20:32:12 GMT Content-Length:224 HTTP / 1.1 411 Length Required Content-Type:text / html; charset = us-ascii服务器:Microsoft-HTTPAPI / 2。0日期:2011年10月14日星期五20:32:12 GMT连接:关闭内容长度:344
  长度要求   HTTP错误411.请求必须分块或具有内容长度。

所以我的问题是“请求必须被分块或具有内容长度。” 我的POST是通过使用'Live HTTP Headers'(Mozila)

咨询浏览器帖子而制作的

如果我评论秒数据&amp;打开/发布它工作正常。 秒数据和开放/后期是问题。

任何人都可以帮助我吗? 谢谢你的阅读。

2 个答案:

答案 0 :(得分:2)

除非您看到正在传递的实际数据,否则很难确切知道发送的是什么。

下载fiddler Web代理并将确切的浏览器输出与curl进行比较。

http://www.fiddler2.com/fiddler2/

添加这行PHP代码以通过curl通过fiddler。

curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888'); 

答案 1 :(得分:2)

将content-length标头设置为您要发送的数据的长度。有关示例,请参阅http://www.php.net/manual/en/function.curl-setopt.php#80271