Curl Post - Longitud requerida
-
21-12-2019 - |
Pregunta
He hecho un código simple para iniciar sesión en mi sitio web.
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;
}
Básicamente, un paso normal para mi sistema de inicio de sesión sería:
-
Envíe una solicitud GET para recuperar el token de seguridad e inicializar una sesión de cookies.(Hecho y trabajando)
$security_token = $this->browser->request('https://mysite.com/login', true, null);
-
Envíe una solicitud de publicación con el nombre de usuario, la contraseña y el token de seguridad.
$postdata = array( 'login' => $login, 'password' => $password, '__RequestVerificationToken' => $security_token, ); $login_page = $this->browser->request('https://mysite.com/login', false, $postdata);
En el paso # 2, obtuve un error 411 que dice que se requiere la longitud del contenido.
¿Hice algo mal o me olvidé de establecer un parámetro?
Solución
La solución fue eliminar esta línea
$options[CURLOPT_CUSTOMREQUEST] = 'POST';
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow