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.
– brazilianldsjaguar
With only I.E in compatibility mode, in other (FF, Chrome and Safari) the page turns all white when I access the form link.
– Gustavo Costa
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.
– brazilianldsjaguar
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.?
– Gustavo Costa