title: php cURL - The request must be chunked or have a content length
body: <p>today I was trying something using 'lib cURL' when I got this weird error, I cannot found a solution for this, can you help me to figure it out?</p>
  
  <pre><code>&lt;?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-&gt;cookie_f = $cookie;
        }
    }
  
    function setproxy($proxy)
    {
        list($ip, $porta) = explode(":", $proxy);
        $this-&gt;proxy_ip = $ip;
        $this-&gt;proxy_port = $porta;
    }
  
    function setcookie($cookie)
    {
        $this-&gt;cookie = $cookie;
    }
  
    function open($type, $url)
    {
        $this-&gt;ch = curl_init();
        curl_setopt($this-&gt;ch, CURLOPT_URL, $url);      
        curl_setopt($this-&gt;ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($this-&gt;ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this-&gt;ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($this-&gt;ch, CURLOPT_HEADER, true);
        //curl_setopt($this-&gt;ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded", "Content-length: 216"));
        curl_setopt($this-&gt;ch, CURLOPT_ENCODING, "");
        curl_setopt($this-&gt;ch, CURLOPT_COOKIEFILE, getcwd() . '/cookies.cookie');
        curl_setopt($this-&gt;ch, CURLOPT_COOKIEJAR, getcwd() . '/cookies.cookie');
        curl_setopt($this-&gt;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-&gt;ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($this-&gt;ch, CURLOPT_POST, 1);
            curl_setopt($this-&gt;ch, CURLOPT_POSTFIELDS, $this-&gt;data);
            curl_setopt($this-&gt;ch, CURLOPT_HEADER, true);
        }
  
        $this-&gt;content = curl_exec($this-&gt;ch);
        curl_close($this-&gt;ch);
        return $this-&gt;content;
  
    }
    }
  
  ?&gt;
  &lt;?php
      $curll = New cURL;
      $curll -&gt;setCookie = "cookies.cookie";
      $curll -&gt;data = "username=user&amp;password=pass&amp;IsSyncUp=&amp;FacebookAssociation=&amp;SNAccessToken=";
      $curll -&gt;open("POST", "http://www.website.com");
      $curll -&gt;data = '__EVENTTARGET=ctl00$ctl00$cphRoblox$cphMyRobloxContent$lbSend&amp;ctl00$ctl00$cphRoblox$cphMyRobloxContent$rbxMessageEditor$txtSubject=AAAAAAAAA&amp;ctl00$ctl00$cphRoblox$cphMyRobloxContent$rbxMessageEditor$txtBody=BBBBBBBBB';
      $curll -&gt;open("POST", "http://www.website.com/My/PrivateMessage.aspx?RecipientID=20815603");
      echo $curll -&gt;content;
  ?&gt;
  </code></pre>
  
  <p>So, my returned error was:</p>
  
  <blockquote>
    <p>HTTP/1.1 302 Found Cache-Control: private Content-Type: text/html; charset=utf-8 Location: /Login/Default.aspx?ReturnUrl=%2fMy%2fPrivateMessage.aspx%3fRecipientID%3d20815603&amp;RecipientID=20815603 Server: Microsoft-IIS/7.0 Set-Cookie: rbx-ip=; path=/; 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&amp;rbx_campaign=&amp;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 OUR IND PHY ONL UNI COM NAV INT DEM PRE" Date: Fri, 14 Oct 2011 20:32:12 GMT Content-Length: 224 HTTP/1.1 411 Length Required Content-Type: text/html; charset=us-ascii Server: Microsoft-HTTPAPI/2.0 Date: Fri, 14 Oct 2011 20:32:12 GMT Connection: close Content-Length: 344<br>
    Length Required 
    HTTP Error 411. The request must be chunked or have a content length.</p>
  </blockquote>
  
  <p>So my problem was "The request must be chunked or have a content length."
  My POST was made by consulting browser post using 'Live HTTP Headers' (Mozila)</p>
  
  <p>If I comment the seconds data &amp; open/post it works fine.
  The seconds data and open/post was the problem.</p>
  
  <p>Can anybody help me?
  Thanks for reading.</p>