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);