forked from azure-appservice-samples/phpemptysite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSinesp.php
149 lines (121 loc) · 3.54 KB
/
Sinesp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
class Sinesp
{
private $secret = 'TRwf1iBwvCoSboSscGne';
private $url = 'https://sinespcidadao.sinesp.gov.br/sinesp-cidadao/mobile/consultar-placa';
private $proxy = null;
private $placa = '';
private $response = '';
private $dados = [];
public function buscar($placa, array $proxy = [])
{
if ($proxy) {
$this->proxy($proxy['ip'], $proxy['porta']);
}
$this->setUp($placa);
$this->exec();
}
public function dados()
{
return $this->dados;
}
public function proxy($ip, $porta)
{
$this->proxy = $ip . ':' . $porta;
}
public function __get($name)
{
return array_key_exists($name, $this->dados) ? $this->dados[$name] : '';
}
public function existe()
{
return array_key_exists('codigoRetorno', $this->dados) && $this->dados['codigoRetorno'] != '3';
}
private function exec()
{
$this->verificarRequisitos();
$this->obterResposta();
$this->tratarResposta();
}
private function obterResposta()
{
$xml = $this->xml();
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"Content-length: ".strlen($xml),
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $this->url);
if ($this->proxy) {
curl_setopt($ch, CURLOPT_PROXY, $this->proxy);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$this->response = curl_exec($ch);
curl_close($ch);
}
private function tratarResposta()
{
$response = str_ireplace(['soap:', 'ns2:'], '', $this->response);
$this->dados = (array) simplexml_load_string($response)->Body->getStatusResponse->return;
}
private function verificarRequisitos()
{
if (! function_exists('curl_init')) {
throw new \Exception('Incapaz de processar. PHP requer biblioteca cURL');
}
if (! function_exists('simplexml_load_string')) {
throw new \Exception('Incapaz de processar. PHP requer biblioteca libxml');
}
return;
}
private function setUp($placa)
{
$placa = $this->ajustar($placa);
if (! $this->validar($placa)) {
throw new \Exception('Placa do veiculo nao especificada ou em formato invalido!');
}
$this->placa = $placa;
}
private function token()
{
return hash_hmac('sha1', $this->placa, $this->placa . $this->secret);
}
private function xml()
{
$xml=<<<EOX
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<v:Envelope xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header>
<b>samsung GT-I9192</b>
<c>ANDROID</c>
<d>6.0.1</d>
<e>4.1.5</e>
<f>10.0.0.1</f>
<g>%s</g>
<l>%s</l>
<m>8797e74f0d6eb7b1ff3dc114d4aa12d3</m>
</v:Header>
<v:Body>
<n0:getStatus xmlns:n0="http://soap.ws.placa.service.sinesp.serpro.gov.br/">
<a>%s</a>
</n0:getStatus>
</v:Body>
</v:Envelope>
EOX;
return sprintf($xml, $this->token(), strftime('%Y-%m-%d %T'), $this->placa);
}
private function validar($placa)
{
return preg_match('/^[a-zA-Z]{3}-?\d{4}$/i', $placa);
}
private function ajustar($placa)
{
return str_replace(['-', ' '], '', $placa);
}
}