Advertisement
kinhoSilva

Sinesp.php

Feb 6th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.29 KB | None | 0 0
  1. <?php
  2. class Sinesp
  3. {
  4.     private $secret = '#8.1.0#g8LzUadkEHs7mbRqbX5l';
  5.     private $url = 'https://cidadao.sinesp.gov.br/sinesp-cidadao/mobile/consultar-placa/v4';
  6.     private $proxy = null;
  7.     private $placa = '';
  8.     private $response = '';
  9.     private $dados = [];
  10.     /**
  11.      * Time (in seconds) to wait for a response
  12.      * @var int
  13.      */
  14.     private $timeout = null;
  15.  
  16. /***
  17.          AQUI ESTA O METODO QUE REALIZA O LOOP
  18. ***/
  19.     public function buscarProxy(string $placa, array $proxy){
  20.  
  21.         $i=0;
  22.         do{
  23.             $response = $this->buscar($placa, $proxy[$i]);
  24.  
  25.             if($this->response){
  26.                 return $response;
  27.                 break;
  28.             }
  29.             $i++;
  30.         }while($i < count($proxy));
  31.     }
  32.  
  33.     public function buscar($placa, $proxy= null)
  34.     {
  35.         if ($proxy) {
  36.             $this->proxy($proxy);
  37.         }
  38.         $this->setUp($placa);
  39.         $this->exec();
  40.         return $this;
  41.     }
  42.     public function dados()
  43.     {
  44.         if(!$this->dados){
  45.             throw new Exception("O servidor retornou nenhuma resposta!", 1);
  46.         }
  47.  
  48.         return $this->dados;
  49.     }
  50.     public function proxy($proxy)
  51.     {
  52.         $this->proxy = $proxy;
  53.     }
  54.     /**
  55.      * Set a timeout for request(s) that will be made
  56.      * @param  int  $seconds How much seconds to wait
  57.      * @return self
  58.      */
  59.     public function timeout($seconds)
  60.     {
  61.         $this->timeout = $seconds;
  62.         return $this;
  63.     }
  64.     public function __get($name)
  65.     {
  66.         return array_key_exists($name, $this->dados) ? $this->dados[$name] : '';
  67.     }
  68.     public function existe()
  69.     {
  70.         return array_key_exists('codigoRetorno', $this->dados) && $this->dados['codigoRetorno'] != '3';
  71.     }
  72.     private function exec()
  73.     {
  74.         $this->verificarRequisitos();
  75.         $this->obterResposta();
  76.         $this->tratarResposta();
  77.     }
  78.     private function obterResposta()
  79.     {
  80.         $xml = $this->xml();
  81.         $headers = [
  82.             'Content-type: text/xml;charset="utf-8"',
  83.             'Accept: text/xml',
  84.             'Cache-Control: no-cache',
  85.             'Pragma: no-cache',
  86.             'Content-length: ' . strlen($xml),
  87.         ];
  88.         $ch = curl_init();
  89.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  90.         curl_setopt($ch, CURLOPT_URL, $this->url);
  91.         if ($this->proxy) {
  92.             curl_setopt($ch, CURLOPT_PROXY, $this->proxy);
  93.         }
  94.         if ($this->timeout) {
  95.             curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout);
  96.             curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
  97.         }
  98.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  99.         curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  100.         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  101.         $this->response = curl_exec($ch);
  102.         curl_close($ch);
  103.     }
  104.     private function tratarResposta(){
  105.         if ($this->response){
  106.             $response = str_ireplace(['soap:', 'ns2:'], '', $this->response);
  107.             $this->dados = (array) simplexml_load_string($response)->Body->getStatusResponse->return;
  108.         }
  109.     }
  110.  
  111.     private function verificarRequisitos()
  112.     {
  113.         if (!function_exists('curl_init')) {
  114.             throw new \Exception('Incapaz de processar. PHP requer biblioteca cURL');
  115.         }
  116.         if (!function_exists('simplexml_load_string')) {
  117.             throw new \Exception('Incapaz de processar. PHP requer biblioteca libxml');
  118.         }
  119.         return;
  120.     }
  121.     private function setUp($placa)
  122.     {
  123.         $placa = $this->ajustar($placa);
  124.         if (!$this->validar($placa)) {
  125.             throw new \Exception('Placa do veiculo nao especificada ou em formato invalido!');
  126.         }
  127.         $this->placa = $placa;
  128.     }
  129.     private function token()
  130.     {
  131.         return hash_hmac('sha1', $this->placa, $this->placa . $this->secret);
  132.     }
  133.     private function xml()
  134.     {
  135.         $xml = <<<EOX
  136. <?xml version="1.0" encoding="utf-8" standalone="yes" ?>
  137. <v:Envelope xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
  138. <v:Header>
  139. <b>samsung GT-I9192</b>
  140. <c>ANDROID</c>
  141. <d>8.1.0</d>
  142. <i>%s</i>
  143. <e>4.1.5</e>
  144. <f>10.0.0.1</f>
  145. <g>%s</g>
  146. <k></k>
  147. <h>%s</h>
  148. <l>%s</l>
  149. <m>8797e74f0d6eb7b1ff3dc114d4aa12d3</m>
  150. </v:Header>
  151. <v:Body>
  152. <n0:getStatus xmlns:n0="http://soap.ws.placa.service.sinesp.serpro.gov.br/">
  153. <a>%s</a>
  154. </n0:getStatus>
  155. </v:Body>
  156. </v:Envelope>
  157. EOX;
  158.         return sprintf($xml, $this->latitude(), $this->token(), $this->longitude(), strftime('%Y-%m-%d %H:%M:%S'), $this->placa);
  159.     }
  160.     private function validar($placa)
  161.     {
  162.         return preg_match('/^[a-zA-Z]{3}-?\d{4}$/i', $placa);
  163.     }
  164.     private function ajustar($placa)
  165.     {
  166.         return str_replace(['-', ' '], '', $placa);
  167.     }
  168.     private function latitude()
  169.     {
  170.         return '-38.5' . rand(100000, 999999);
  171.     }
  172.     private function longitude()
  173.     {
  174.         return '-3.7' . rand(100000, 999999);
  175.     }
  176. }
  177.  
  178. /***
  179.         EXEMPLO DE USO
  180. ***/
  181. $veiculo = new Sinesp;
  182.  
  183. try{
  184.     $veiculo->buscarProxy('GWW-6471', ['187.95.251.130:8081', '187.95.251.130:8080', '187.95.251.130:8280']);
  185.     echo json_encode($veiculo->dados());
  186. }catch(Exception $e){
  187.     echo json_encode(['codigoErro' => $e->getCode(), 'menssagem' =>$e->getMessage(), 'Linha' => $e->getLine()]);
  188. }
  189. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement