CURL Post - 需要的长度

时间:2014-02-25 19:13:29

标签: php cookies curl http-status-code-411

我已经制作了一个简单的代码来登录我的网站。

private function request($url, $reset_cookies, $post_data)
  {

    $options = array(
      CURLOPT_URL               => $url,
      CURLOPT_RETURNTRANSFER    => 1,
      CURLOPT_HEADER            => 0,
      CURLOPT_FAILONERROR       => 1,
      CURLOPT_USERAGENT         => $this->user_agent,
      CURLOPT_CONNECTTIMEOUT    => 30,
      CURLOPT_TIMEOUT           => 30,
      CURLOPT_SSL_VERIFYPEER    => 0,   
      CURLOPT_FOLLOWLOCATION    => 1,
      CURLOPT_MAXREDIRS         => 10,
      CURLOPT_AUTOREFERER       => 1,
      CURLOPT_COOKIESESSION     => $reset_cookies ? 1 : 0,
      CURLOPT_COOKIEJAR         => $this->session_id,
      CURLOPT_COOKIEFILE        => $this->session_id,
    );

    // Add POST data
    if (isset($post_data))
    {
      $options[CURLOPT_CUSTOMREQUEST] = 'POST';
      $options[CURLOPT_POST]          = 1;
      $options[CURLOPT_POSTFIELDS]    = http_build_query($post_data);
    }

    // Attach options
    curl_setopt_array($this->curl, $options);

    // Execute the request and read the response
    $content = curl_exec($this->curl);

    // Handle any error
    if (curl_errno($this->curl)) throw new Exception(curl_error($this->curl));

    return $content;
  }

基本上,我的登录系统的正常步骤是:

  1. 发送GET请求以检索安全令牌并初始化cookie会话。 (完成和工作)

    $security_token = $this->browser->request('https://mysite.com/login', true, null);
    
  2. 使用用户名,密码和安全令牌发送POST请求。

    $postdata = array(
        'login' => $login,
        'password' => $password,
        '__RequestVerificationToken' => $security_token,
    );
    $login_page = $this->browser->request('https://mysite.com/login', false, $postdata);
    
  3. 在第2步,我收到411错误,说明需要内容长度。

    我做错了什么或忘了设置参数?

1 个答案:

答案 0 :(得分:2)

解决方法是删除此行

$options[CURLOPT_CUSTOMREQUEST] = 'POST';