PHP - cURL returning error 'Microsoft VBScript runtime error' 800a01a8 'object required:' Session (...) '"

0

I'm developing an application where several automations (all sending forms, relatively simple) will be made to external sites using cURL. I've been successful in at least five cases, except the current one. I can log in and catch cookies normally, but when I open the next page, the error described in the title is returned.

Follow my code:

Step 01 - Log in

$curl = curl_init('http://site.com.br/login.asp');
$fields = array(
'usuario'=>urlencode("usuario_login"),
'senha'=>urlencode("senha_login")
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&' ; }
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($curl, CURLOPT_COOKIEJAR, $_SERVER['DOCUMENT_ROOT']."/cookie.txt"); //GRAVO OS COOKIES EM UM ARQUIVO TXT
$resultado = curl_exec($curl);
curl_close($curl);
echo $resultado;

So far so good, the welcome screen is returned. Then I go to the second page, where the form to fill in exists.

Step 02 - Fill out the form

$curl = curl_init('http://site.com.br/formulario.asp');
$fields = array(
'campo_01'=>urlencode("dado_campo_01"),
'campo_02'=>urlencode("dado_campo_02"),
'campo_03'=>urlencode("dado_campo_03"),
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&' ; }
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($curl, CURLOPT_COOKIEFILE, $_SERVER['DOCUMENT_ROOT']."/cookie.txt"); //LEIO OS COOKIES DO ARQUIVO TXT
$resultado_final = curl_exec($curl);
curl_close($curl);
echo $resultado_final;

Then the following header and error is returned:

HTTP/1.1 500 Internal Server Error 
Date: Mon, 31 Aug 2015 20:27:46 GMT 
Server: Microsoft-IIS/6.0 
X-Powered-By: ASP.NET 
Content-Length: 321 
Content-Type: text/html 
Set-Cookie: ASPSESSIONID=EODOFMOBPPIFPLGDPPNCGMCL; path=/ Cache-control: private

Microsoft VBScript runtime error '800a01a8'

Object required: 'Session(...)'

Some attempts I've already made:

I have tried several options, I am two days searching in various forums, and no matter what I do or what CURLOPT I use, the error is always the same. I've even read all the cURL documentation to see if it had any CURLOPT that might be useful.

I have tried to set the cookie manually with CURLOPT_COOKIE with the value that is returned in the header (ASPSESSIONID = EODOFMOBPPIFPLGDPPNCGMCL). Detail that the value of this session changes every request that I make the page.

I've just tried to display the form.asp page, without POST (removing POSTSFIELDS).

I do not know if the error is in the cookie or if the hole is lower. Looking at the code, can you identify any mistakes I made?

Headers I've set

$headers = array();
$headers[] = 'X-Powered-By: ASP.NET';
$headers[] = 'Server: Microsoft-IIS/6.0';
$headers[] = 'Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, */*';
$headers[] = 'Accept-Encoding: gzip, deflate';
$headers[] = 'Accept-Language: pt-BR';
$headers[] = 'Cache-Control: no-cache';
$headers[] = 'Content-Type: application/x-www-form-urlencoded;';
$headers[] = 'Host: site.com.br';
$headers[] = 'Referer: http://site.com.br/formulario.asp';
$headers[] = 'User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729)';
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    
asked by anonymous 31.08.2015 / 23:55

1 answer

1

I've done a lot of these automations lately and making scripts really simulate human interaction.
PHP + cURL is a bit annoying and there are some details that one must watch because depending on how the remote server analyzes the requests, you need to know these subtleties. Looking superficially at your script, here are some points you should note:

CookieJars

You are sure to log in and create a session with cookiejar, however the next requests you should send them with CURLOPT_COOKIEFILE , in addition to CURLOPT_COOKIEJAR , this way the cookies will be resent if needed, such as the browser. Then the first request you send only with CURLOPT_COOKIEJAR and subsequent ones with CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE .

POST data

Another subtlety here: If you switch to CURLOPT_POSTFIELDS an array, the HTTP request will be sent with a Content-Type header as multipart/form-data . On the other hand, if you pass this option (CURLOPT_POSTFIELDS) the parameters like URL-encoded like this:

$post_params = http_build_query(array("param" => "value");

Then Content-type of the request will be application/x-www-form-urlencoded .

Perceive? That being said, if the server evaluates this header and is not quite what it expects, maybe it (the server / application) refuses to respond to what you expect. I also noticed that you are individually "encoding" each POST parameter:

$fields = array(
    'campo_01'=>urlencode("dado_campo_01"),
//...
);

This is not necessary. If you do not use link cURL will take care of this for you.

HTTP Headers

Do not push your luck!

Always start the script by simulating exactly (or at least next) the headers required for the HTTP request. Use Chrome Developer Tools or Firefox Dev Tools and check the HTTP headers of the request you want to simulate with PHP. At a minimum there should be at least one header informing the User-Agent. Often the application checks the User-Agent and also the header Referer .

    
01.09.2015 / 00:44