Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Boa noite a todos
Eu gostaria de saber se é possível usar curl para integrar a sistemas que usam multiplos formulários como é o caso do sistema de agendamento eletrônico do INSS.
Como seria?
Grato
como até agora ninguem respondeu, resolvi postar o código
fonte do código: http://cacovsky.wordpress.com/2011/02/06/php-curl-cookies-sessions-cpf-e-site-da-receita-federal/
arquivo formulario_ag.php
<?php
session_start();
//separador de diretorio
$dir_sep = DIRECTORY_SEPARATOR;
$cookie_file = tempnam (realpath(sys_get_temp_dir()), "CURLCOOKIE");
$_SESSION['cookie_file'] = substr($cookie_file, strrpos (tempnam (realpath(sys_get_temp_dir()), "CURLCOOKIE"), $dir_sep));
?>
<html>
<head>
<title>Agendamento</title>
</head>
<body>
<?php
$my_site = "[http://localhost/agendamento/](http://localhost/agendamento/)";
//parte 1: acessar o site e obter o cookie do captcha apropriadamente
$url_imagem = "[http://www2.dataprev.gov.br/prevagenda/captcha](http://www2.dataprev.gov.br/prevagenda/captcha)";
$ch = curl_init ();
curl_setopt ($ch, CURLOPT_URL, $url_imagem);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch); // the real thing
curl_close($ch);
$img_file_name = tempnam ("images/", "CURLGIF_");
$img_data = imagecreatefromstring($output);
imagegif($img_data, $img_file_name);
$img_link = $my_site."images".substr( $img_file_name, strrpos ($img_file_name , $dir_sep));
//detectando sistema operacional
$os = strtoupper(php_uname('s'));
//eh windows
if (strpos($os, 'WIN')===TRUE) {
$img_link = str_replace('\\','/',$img_link);
}
?>
<form method="post" action="consultar_ag.php">
<input name="opcaoAgendarConsultar" value="agendar" checked="checked" type="radio">
Captcha: <input type="text" name="robot"/> <br />
<img src="<?php echo $img_link ?>" /> <br/>
<input name="enviar" id="enviar" value="Avançar" type="submit">
</form>
</body>
</html>
consultar_ag.php
<?php
session_start();
//parte 2: realizar a requisição utilizando o mesmo cookie
$url_requisicao = "[http://www2.dataprev.gov.br/prevagenda/OpcaoInicial.view](http://www2.dataprev.gov.br/prevagenda/OpcaoInicial.view)";
$campos = $_POST;
$campos_concat = '';
$cookie_file = realpath(sys_get_temp_dir()).$_SESSION['cookie_file'];
$campos_concat = 'opcaoAgendarConsultar='.$_POST['opcaoAgendarConsultar'].'&robot='.$_POST['robot'];
$reffer = "[http://www2.dataprev.gov.br/prevagenda/OpcaoInicialTela.view](http://www2.dataprev.gov.br/prevagenda/OpcaoInicialTela.view)";
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url_requisicao);
curl_setopt($ch,CURLOPT_REFERER,$reffer);
curl_setopt($ch,CURLOPT_USERAGENT,$agent);
curl_setopt($ch,CURLOPT_POST, count($_POST));
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$campos_concat);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie_file);// grava o novo cookie
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
$result = iconv("ISO-8859-1","UTF-8",$result);
//close connection
curl_close($ch);
//echo $result;
header("location: servico.php");
?>
servico.php
<?php
session_start();
// realiza a requisição utilizando o novo cookie
$servico = "BSAIR";
$url_requisicao = "[http://www2.dataprev.gov.br/prevagenda/Servico.view](http://www2.dataprev.gov.br/prevagenda/Servico.view)";
$campos = $_POST;
$campos_concat = '';
$cookie_file = realpath(sys_get_temp_dir()).$_SESSION['cookie_file'];
$campos_concat = 'servico='.$servico;
$reffer = "[http://www2.dataprev.gov.br/prevagenda/OpcaoInicialTela.view](http://www2.dataprev.gov.br/prevagenda/OpcaoInicialTela.view)";
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url_requisicao);
curl_setopt($ch,CURLOPT_REFERER,$reffer);
curl_setopt($ch,CURLOPT_USERAGENT,$agent);
curl_setopt($ch,CURLOPT_POST, count($_POST));
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$campos_concat);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
$result = iconv("ISO-8859-1","UTF-8",$result);
//close connection
curl_close($ch);
//echo $result;
header("location: requerimento.php");
?>
descomente echo $result; em cada arquivo para ver o retorno
não estã passando desse
requerimento.php
<?php
session_start();
// realiza a requisição utilizando o novo cookie
$nome = "JOSE MENEZES PORTELA";
$dataNascimento = "03/04/1953";
$numero = "16893648627";
$url_requisicao = "www2.dataprev.gov.br/prevagenda/requerimentoNit.view";
$campos = $_POST;
$campos_concat = '';
$cookie_file = realpath(sys_get_temp_dir()).$_SESSION['cookie_file'];
$campos_concat = 'nome='.$nome.'&dataNascimento='.$dataNascimento.'&numero='.$numero;
$reffer = "www2.dataprev.gov.br/prevagenda/Servico.view";
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
$ch = curl_init();
curl_setopt($ch,CURLOPT_REFERER,$reffer);
curl_setopt($ch,CURLOPT_USERAGENT,$agent);
curl_setopt($ch,CURLOPT_URL,$url_requisicao);
curl_setopt($ch,CURLOPT_POST, count($_POST));
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$campos_concat);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
$result = iconv("ISO-8859-1","UTF-8",$result);
//close connection
curl_close($ch);
echo $result;
//echo $cookie_file;
?>A causa está aqui:
onclick="confirmar()
<script language="javascript"> function confirmar(){ document.forms[0].submit(); } </script>
Alguem sabe se tem como passar disso?
Descobri que ao validar o captcha, na próxima página, o cookie muda, daí é usado o mesmo cookie até o fim.
Consegui exibir a próxima página e a seguinte, mais aí chegou uma que da 500 internal server error.
É como se estivesse acessando a página sem enviar o cookie, mas o cookie está sendo enviado.
Alguem saberia dizer porque?
Grato