<?php
// inclui o diretório do ZF no include_path
$pathToZend = dirname(__FILE__) . '/../ZendFramework-1.10.8/library/';
set_include_path(get_include_path() . PATH_SEPARATOR
. $pathToZend);
include_once('Zend/Http/Client.php');
include(dirname(__FILE__) . '/EleitorModel.php');
class EleitorSpider
{
private $client;
public function __construct
() {
$this->client = new Zend_Http_Client
();
$this->client->setConfig(array('timeout' => 5));
$this->client->setCookieJar();
}
public function consultaSituacao
($nome, $dataNascimento) {
try
{
if (trim($nome) == '') throw new Exception
('Nome não enviado!');
if (trim($dataNascimento) == '') throw new Exception
('Data de Nascimento não enviada!');
$token = $this->step1($nome, $dataNascimento);
return $this->step2($token, $nome, $dataNascimento);
} catch
(Exception
$e) {
throw $e;
}
}
private function step1
() {
try
{
$ref = "http://www.tse.gov.br/internet/servicos_eleitor/consultaSituacaoNome.htm";
$url = "http://www.tse.gov.br/certidaoquitacao/consultaNomeDataNascimento.do?dispatcher=exibirConsultaNomeDataNascimento&validate=false";
$this->client->resetParameters();
$this->client->setHeaders('Referer', $ref);
$this->client->setUri($url);
$response = $this->client->request('GET');
$result = $response->getBody();
if ($response->isError()) throw new Exception
("Erro na requisicao HTTP do Metodo 'step1': " . $response->getStatus());
return $this->step1Parse($result);
} catch
(Exception
$e) {
throw $e;
}
}
function step1Parse
($result) {
$pattern = '@<input\s*type="hidden"\s*name="org\\.apache\\.struts\\.taglib\\.html\\.TOKEN"\s*value="(.*?)">@i';
$int = preg_match($pattern, $result, $matches);
if (count($matches) == 0) throw new Exception
("Erro ao recuperar token!");
return $matches[1];
}
private function step2
($token, $nome, $dataNascimento) {
try
{
$ref = "http://www.tse.gov.br/internet/servicos_eleitor/consultaSituacaoNome.htm";
$url = "http://www.tse.gov.br/certidaoquitacao/consultaNomeDataNascimento.do";
$this->client->resetParameters();
$this->client->setHeaders('Referer', $ref);
$this->client->setUri($url);
$postData = array( 'org.apache.struts.taglib.html.TOKEN' => $token,
'dispatcher' => "consultarNomeDataNascimento",
'validate' => 'true',
'nomeEleitor' => $nome,
'dataNascimento' => $dataNascimento,
'consultar' => 'Consultar' );
$this->client->setParameterPost($postData);
$response = $this->client->request('POST');
$result = $response->getBody();
if ($response->isError()) throw new Exception
("Erro na requisicao HTTP do Metodo 'step1': " . $response->getStatus());
return $this->step2Parse($result);
} catch
(Exception
$e) {
throw $e;
}
}
function step2Parse
($result) {
$pattern = '@Os\s*dados\s*informados\s*\(nome,\s*data\s*de\s*nascimento\s*e/ou\s*filia.*?o\)\s*n.*?o\s*conferem@i';
if (preg_match($pattern, $result)) throw new Exception
("Os dados informados não existem no Cadastro Eleitoral.");
$patterns = array( 'titulo' => '@<label[^>]*?>T.*?tulo\s*de\s*Eleitor:</label>\s*(.*?)\s*<br\s*/>@i',
'nome' => '@<label[^>]*?>Nome\s*do\s*Eleitor:</label>\s*(.*?)\s*<br\s*/>@i',
'dataNascimento' => '@<label[^>]*?>Data\s*de\s*Nascimento:</label>\s*(.*?)\s*<br\s*/>@i',
'situacao' => '@<label[^>]*?>Situa.*?o\s*da\s*Inscri.*?o:</label>\s*(.*?)\s*<br\s*/>@is' );
$o = new EleitorModel
();
foreach ($patterns as $campo => $p) {
$int = preg_match($p, $result, $matches);
if (count($matches) == 0) throw new Exception
("Não foi possível recuperar '$campo'");
eval("\$o->$campo = \$matches[1];");
}
return $o;
}
}
?>
Add a code snippet to your website: paste.org