PHP - Curl returning error "Microsoft Vbscript Runtime error '800a01a8' Object required: 'Session(...)'"

Asked

Viewed 196 times

0

I am developing an application where various automations will be made (all forms submission, relatively simple) on external websites using Curl. I’ve had success in at least five cases, except for the current one. I can log in and capture cookies normally, but when opening the next page, the error described in the title is returned.

Follows 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, is returned the welcome screen. So I go to the second page, where there is the form to fill.

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 is returned the header and error below:

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 which CURLOPT I use, the error is always the same. In fact, I read all the Curl documentation to see if there was 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 to each request I make the page.

I have tried to just display the page form.Asp, without doing POST (removing the POSTSFIELDS).

I don’t know if the mistake is in the cookie or if the hole is further down. Looking at the code, you can identify some mistake I made?


Headers I defined

$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);
  • Can you do what you want without Curl on that same site? For example, using any browser? It could just be a mistake on their websites.

  • With only I.E in compatibility mode, in other (FF, Chrome and Safari) the page turns all white when I access the form link.

  • This error is from the site - if you are not the owner of the site, I suggest to contact you and warn you that its site has a Vbscript error when logging in. Curl simply returns what the site returns. If it was a Curl error, it would have returned a PHP exception itself.

  • In I.E. in compatibility mode I can use the site without any error. There is some way to make Curl "simulate" this compatibility mode of I.E.?

1 answer

1

I’ve been doing a lot of these automations lately and making scripts really simulate human interaction.
PHP+Curl is a bit boring and there are some details you should note 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 right to log in and create a session with cookiejar, however the next requests you should send them with CURLOPT_COOKIEFILE, besides the CURLOPT_COOKIEJAR, so session cookies will be forwarded if necessary, just like the browser. So the first request you send only with CURLOPT_COOKIEJAR and subsequent with CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE.

POST data

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

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

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

Do you understand? That being said, if the server evaluates this header and is not exactly what it expects, maybe it (the server/application) denies responding 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 http_build_query() Curl will take care of it for you.

Headers HTTP

Don’t push your luck!

Always start the script by simulating exactly (or at least near) 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 the very least, there should be at least one header informing the User-Agent. Often the application checks the User-Agent and also the header Referer.

  • Thanks for the tips, I followed your recommendations and the error persists. Cookiejar - I did exactly as you recommended POSTDATA - I used http_build_query. Headers - using Chrome Dev Tools (and also by IE, since the site only works on IE), I was able to view the headers of the page, so I set the following options: (I will edit my question here in the comments exceeded the character limit)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.