Oct 14
Olá pesÂsoal depois de um tempo sem posÂtar surÂgiu uma necesÂsiÂdade de rasÂtrear minhas encoÂmenÂdas nos Correios.
Como não gosto de ficar aperÂtando F5 no site dos CorÂreios que vive com proÂbleÂmas resolvi fazer a conÂsulta via Ajax. O codigo é simÂples e aproÂveiÂtei a ideia do sisÂtema para calÂcuÂlar frete.
Segue exemÂplo funÂciÂoÂnando.
</p>
<?php
if($_POST) {
header("Content-Type: text/html; charset=ISO-8859-1");
$p_cod_lis = $_POST['P_COD_LIS'];
if($p_cod_lis == '') {
echo '<center>Código não informado</center>';
}
$url = "http://websro.correios.com.br/sro_bin/txect01$.Inexistente?P_LINGUA=001&P_TIPO=002&P_COD_LIS=$p_cod_lis";
$conecurl = @fopen("$url","r") or die ('<center>erro na conexão</center>');
while(!feof($conecurl)) {
$lin .= fgets($conecurl,4096);
}
fclose($conecurl);
$lin = strtolower($lin);
$rest = substr($lin,0);
$nprimetable = strpos($rest,'<table ');
$fechatable = strpos($rest,'<hr ');
$quantopula = $fechatable - $nprimetable ;
$conteudo = substr($lin, $nprimetable ,$quantopula);
function get_anchor($html)
{
$er = "/<td.*?>.*?<\/td>/";
preg_match_all($er,$html,$links);
$link = $links[0];
return $link;
}
function get_label($url)
{
$label = str_replace("</td>","",preg_replace("/^<td.*?>/","",$url));
return $label;
}
echo $conteudo;
} else {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link href="http://demo.phaneronsoft.com/img/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<title>Sistema para rastrear entregas no correio. | Phaneronsoft</title>
<style type="text/css">
* {
font-family:"Trebuchet MS","Lucida Grande",Verdana,Tahoma,Helvetica,Arial,sans-serif;
font-size:12px;
font-style:normal;
font-variant:normal;
font-weight:normal;
line-height:normal;
}
body {
background:#FFFFFF none repeat scroll 0 0;
height:100%;
margin:0;
margin-top:20px;
width:100%;
}
div#wrapper {
margin:auto;
position:relative;
width:390px;
z-index:0;
}
.select {
text-transform:uppercase;
width:99%;
border:1px solid #B6B6B6;
display:block;
}
.formSearch fieldset {
border:1px solid #CCCCCC;
margin:0;
padding:0 10px;
}
label {
display:block;
/*float:left;*/
margin-right:4px;
padding-bottom:5px !important;
}
legend {
color:#1E398D;
font-family:"Legacy Sans ITC TT Bold","Trebuchet MS","Lucida Grande",Verdana,Tahoma,Helvetica,Arial,sans-serif;
font-size:1.5em;
padding:10px 5px;
}
.button {
background:#F5EED3;
border:1px solid #CCCCCC;
color:#666666;
cursor:pointer;
font-size:12px;
font-weight:bold;
letter-spacing:1px;
margin:10px 0 0;
overflow:visible;
text-transform:uppercase;
width:150px;
}
.formMain .inline {
margin:15px 0 0 !important;
}
.text, .select, .textarea, .password {
border:1px solid #B6B6B6;
display:block;
text-transform:uppercase;
}
fieldset span.nameField {
color:#666666;
text-transform:uppercase;
}
h3 {
border-bottom:1px solid #F58220;
margin:0;
padding:0;
}
h3 span {
-x-system-font:none;
color:#1E398D;
font-family:"Legacy Sans ITC TT Bold","Trebuchet MS","Lucida Grande",Verdana,Tahoma,Helvetica,Arial,sans-serif;
font-size:1.5em;
font-style:normal;
font-variant:normal;
font-weight:bold;
line-height:normal;
}
#value {
-x-system-font:none;
font-family:"Legacy Sans ITC TT Bold","Trebuchet MS","Lucida Grande",Verdana,Tahoma,Helvetica,Arial,sans-serif;
font-size:1.5em;
font-style:normal;
font-variant:normal;
font-weight:bold;
line-height:normal;
text-transform:capitalize;
}
#value table {
margin:0;
padding:0;
border:0;
}
#rastrear {
padding-top:30px;
}
#loading {
display:none;
color:#9B0000;
font-family:"Legacy Sans ITC TT Bold","Trebuchet MS","Lucida Grande",Verdana,Tahoma,Helvetica,Arial,sans-serif;
font-size:1.5em;
font-style:normal;
font-variant:normal;
font-weight:bold;
line-height:normal;
text-transform:capitalize;
}
</style>
</head>
<body>
<div id="wrapper">
<h3>
<span>Rastrear pedido</span>
</h3>
<form method="post" action="" name="rastreamentohome" onsubmit="submitForm(this); return false;">
<fieldset>
<legend>Identificador do objeto:</legend>
<label for="P_COD_LIS">
<textarea cols="48" id="P_COD_LIS" rows="3" name="P_COD_LIS"></textarea>
</label>
<label for="pesquisar">
<input type="submit" id="pesquisar" name="pesquisar" value="pesquisar" tabindex="3" />
</label>
</fieldset>
</form>
<span id="value"></span>
<span id='loading'>Pesquisando...</span>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js" type="text/javascript"></script>
<script type="text/javascript">
function submitForm(form) {
/*
usa metodo request() da classe Form da prototype, que serializa os campos
do formulario e submete (por POST como default) para a action especificada no form
*/
function mostrarLoading(){
$('loading').style.display = 'block';
$('value').innerHTML = ' ';
}
form.request({
onLoading:mostrarLoading,
onComplete: function(transport){
/*
se o retorno for diferente de -1, entende-se que nao houve problemas, entao apaga-se
os campos do formulario usando o metodo reset() da classe Form
*/
if(transport.responseText !=-1)Â {
$('loading').style.display = 'none';
$('value').innerHTML = transport.responseText;
} else {
form.reset();
$('loading').style.display = 'none';
$('value').innerHTML = 'Erro ao consultar';
}
}
});
return false;
}
</script>
</body>
</html>
<?php } ?>
Caso tenha alguma duvida ou sugesÂtão deixe um comenÂtáÂrio que terei praÂzer em ajuÂdar.
ComenÂtáÂrios são bem vinÂdos assim posso melhoÂrar a quaÂliÂdade dos tutoÂriÂais aqui apresentados.
É isso pesÂsoal uma ótima semana e até o próximo post.
Tags: correios, entregas, rastrearJulio
http://www.phaneronsoft.com MarÂcelo Korjenioski
Dbd_100
http://twitter.com/renatotavares Renato TavaÂres
Juler OliÂveira
Juler OliÂveira