Advertisement
rogerin

class Correios

Feb 8th, 2013
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.42 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class Correio {
  5.  
  6.     public $status;
  7.     public $hash;
  8.     public $erro = false;
  9.     public $track;
  10.  
  11.     /**
  12.     * Construtor
  13.     *
  14.     * @param    string  $id     Código da encomenda
  15.     * return void
  16.     */
  17.     public function __construct($id=false){
  18.         if ($id){
  19.             if (strlen($id) == 13) $this->track ($id);
  20.             else {
  21.                 $this->erro = true;
  22.                 $this->erro_msg = 'Código de encomenda Inválido!';
  23.             }
  24.         }
  25.     }
  26.  
  27.     /**
  28.     * Faz o rastreamendo da encomenda
  29.     *
  30.     * @param    string  $id     Código da encomenda
  31.     * @return   void
  32.     */
  33.     private function track($id){
  34.         $html = utf8_encode(file_get_contents('http://websro.correios.com.br/sro_bin/txect01$.QueryList?P_LINGUA=001&P_TIPO=001&P_COD_UNI=' . $id));
  35.  
  36.         // Verifica se o objeto ainda não foi postado, caso seja o caso, retorna erro e mensagem      
  37.         if (strstr($html, '<table') === false){
  38.             $this->erro = true;
  39.             $this->erro_msg = 'Objeto ainda não foi adicionado no sistema';
  40.             return;
  41.         }
  42.  
  43.         // Hash para monitoramento de alteração de status
  44.         $this->hash = md5($html);
  45.  
  46.         // Limpa o codigo html
  47.         $html = preg_replace("@\r|\t|\n| +@", ' ', $html);
  48.         $html = str_replace('</tr>', "</tr>\n", $html);
  49.  
  50.         // Pega as linhas com o rastreamento
  51.         if (preg_match_all('@<tr>(.*)</tr>@', $html, $mat,PREG_SET_ORDER)){
  52.             $track = array();
  53.             $mat = array_reverse($mat);
  54.             $temp = null;
  55.             // Formata as linhas e gera um vetor
  56.             foreach($mat as $item){
  57.                 if (preg_match("@<td rowspan=[12]>(.*)</td><td>(.*)</td><td><FONT COLOR=\"[0-9A-F]{6}\">(.*)</font></td>@", $item[0], $d)){
  58.                     // Cria uma linha de track
  59.                     $tmp = array(
  60.                         'data' => $d[1],
  61.                         'data_sql' => preg_replace('@([0-9]{2})/([0-9]{2})/([0-9]{4}) ([0-9]{2}):([0-9]{2})@', '$3-$2-$1 $4:$5:00',$d[1] ),
  62.                         'local' => $d[2],
  63.                         'acao' => strtolower($d[3]),
  64.                         'detalhes' => ''
  65.                     );
  66.  
  67.                     // Se tiver um encaminhamento armazenado
  68.                     if ($temp){
  69.                         $tmp['detalhes'] = $temp;
  70.                         $temp = null;
  71.                     }
  72.  
  73.                     // Adiciona o item na lista de rastreamento
  74.                     $track[] = (object)$tmp;
  75.                 }else if (preg_match("@<td colspan=2>(.*)</td>@", $item[0], $d)){
  76.                     // Se for um encaminhamento, armazena para o proximo item
  77.                     $temp = $d[1];
  78.                 }
  79.                 $this->status = $tmp['acao'];
  80.             }
  81.             $this->track = $track;
  82.             return;
  83.         }
  84.  
  85.         // Caso retorne um html desconhecido ou falhe, retorna erro de comunicação
  86.         $this->erro = true;
  87.         $this->erro_msg = 'Falha de Comunicação com os correios';
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement