简体   繁体   中英

Http 411 php curl due to post request

With this code:

<?php

function request($url, $post, $cook)
{
$ch = curl_init();
$cookie_file_path = "vskcookies.txt";

$urll = 'http://auto.vsk.ru/login.aspx';

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, $urll);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_USERAGENT,'"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; Trident/7.0; Touch; .NET4.0C; .NET4.0E; Tablet PC 2.0)"');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $urll);
curl_setopt($ch,  CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

curl_setopt( $ch, CURLOPT_COOKIESESSION, true );

$result = curl_exec($ch);

echo "FIELDS:\n\n".$post;
echo "\n\nHEADERS:\n\n";

curl_close($ch);
return $result;
}
$result = request($_POST['url'], $_POST['data'], $_POST['cook']);


if ($result === FALSE) 
 echo('error');
else
 echo($result);


?>

I am getting two cookies as I need but with http 411 error in body:

在此处输入图片说明

The same request, the same code, but in the end, right before curl_exec I add

curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form- urlencoded;charset=utf-8"));

As the result, I am getting correct body, but now only one cookie (I need both):

在此处输入图片说明

Another variants:

This code

curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Cache-Control: no-cache;Content-Type: application/x-www-form- urlencoded;charset=utf-8;Content-Length: '.strlen($post)));

Gives 411 error and correct cookies for reason.

This

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length:'.strlen($post)));

Causes nothing: just nothing happens.

Also after both (first)variants, vskcookies.txt contains only one (pool) cookie.

Why that?

It looks like, when I add header it erases request's body(post fields).


UPDATE

For this code

curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Cache-Control: no-cache;Content-Type: application/x-www-form- urlencoded;'));

I just sitting and clicking on button that makes ajax request of the script above on my page, getting every time 411 error response (also cookies/time in header updates every time), but after about 10 clicks I got http 200 and the page I need. Then again many times 411 and then again a single 200. By the way cookies file still has only one cookie.

Wtf?

As I mentioned in question's update, sometimes I get 200 response with two cookies and body I need.

So I wrote this is bad unstable solution, often it can remain about 20 seconds:

<?php

function request($url, $post, $cook)
{
$sta=0;

while($sta != '200')
{
$ch = curl_init();
$cookie_file_path = "vskcookies.txt";

$urll = 'http://auto.vsk.ru/login.aspx';

curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_URL, $urll);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_USERAGENT,'"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 10.0; Trident/7.0; Touch; .NET4.0C; .NET4.0E; Tablet PC 2.0)"');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $urll);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt( $ch, CURLOPT_COOKIESESSION, true );
//curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Cache-Control: no-cache;Content-Type: application/x-www-form- urlencoded;'));
//curl_setopt($ch, CURLOPT_POSTREDIR, 2);

$result = curl_exec($ch);

$sta = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);
}

echo "FIELDS:\n\n".$post;
echo "\n\nHEADERS:\n\n";

return $result;
}


$result = request($_POST['url'], $_POST['data'], $_POST['cook']);


if ($result === FALSE) 
 echo('error');
else
 echo($result);

 ?>

Bet there is a much better correct answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM