PHP 2.0
Pré requisitos
Para utilizar a API cliente em PHP, é preciso ter instalado e configurado um servidor WEB, com APACHE e PHP.
Começando
O primeiro passo é ter disponível seu token e a url do sistema. Caso não tenha ainda: Configurando acesso ao Webservice API.
API Client 2.0
A classe WebserviceClient sofreu algumas alterações recentes para corrigir problemas de códificação em requisições utilizando PHP. O código abaixo se trata do código atualizado da mesma.
Código da API client IXC 2.0
1 <?php
2
3 namespace IXCsoft;
4
5 class WebserviceClientException extends \Exception
6 {
7 }
8
9 class WebserviceClient implements \Iterator, \ArrayAccess
10 {
11 private $host, $curl, $token, $selfSigned, $responseBody, $decoded_resposta, $responseHeaders;
12 private $headers = [];
13
14 /**
15 * api constructor.
16 * @param string $host endereço da api
17 * @param string $token token para autenticação é obrigatório
18 * @param bool $selfSigned certificado autoassinado é obrigatório
19 */
20 public function __construct($host, $token = null, $selfSigned = false)
21 {
22 $this->host = $host;
23 $this->token = $token;
24 $this->selfSigned = $selfSigned;
25 $this->curl = curl_init();
26 curl_setopt($this->curl, CURLOPT_HEADER, 1);
27
28 if ($token) {
29 curl_setopt($this->curl, CURLOPT_USERPWD, $token);
30 }
31 curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
32 if ($selfSigned) {
33 curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, FALSE);
34 curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, FALSE);
35 }
36 }
37
38 public function __destruct()
39 {
40
41 curl_close($this->curl);
42 }
43
44 /**
45 * Incluir cabeçalho customizado na requisição
46 * @param string $key Nome do attributo
47 * @param string $value Valor do attributo
48 */
49 public function setCabecalho($key, $value)
50 {
51 $this->headers[] = sprintf("%s: %s", $key, $value);
52 }
53
54 /**
55 * Fazer uma requisição GET
56 * @param string $url endereço da requisição
57 * @param array $params GET parametros da requisição
58 */
59 public function get($url, array $params = [])
60 {
61 $this->headers[] = "ixcsoft: listar";
62 curl_setopt($this->curl, CURLOPT_POST, true);
63 curl_setopt($this->curl, CURLOPT_POSTFIELDS, $params);
64 $this->request($url);
65 }
66
67 /**
68 * Fazer uma requisição POST
69 * @param string $url endereço da requisição
70 * @param string array com o conteúdo
71 * @param bool $json
72 */
73 public function post($url, array $params = [])
74 {
75 curl_setopt($this->curl, CURLOPT_POST, true);
76 curl_setopt($this->curl, CURLOPT_POSTFIELDS, $params);
77 $this->request($url);
78 }
79
80 /**
81 * Fazer uma requisição PUT
82 * @param string $url endereço da requisição
83 * @param mixed $data array com o conteúdo
84 * @param string $registro id do registro
85 */
86 public function put($url, $data, $registro)
87 {
88 if ($json = !is_scalar($data)){
89 $data = json_encode($data);
90 }
91 curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "PUT");
92 curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data);
93 $this->request($url . '/' . $registro, $json);
94 }
95
96 /**
97 * Fazer uma requisição DELETE
98 * @param string $url endereço da requisição
99 * @param string $registro id do registro
100 */
101 public function delete($url, $registro = '')
102 {
103 curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, "DELETE");
104 $this->request($url . '/' . $registro);
105 }
106
107 private function request($target, $json = false)
108 {
109 if (!strpos($target, '&')) {
110 $target = trim($target) . '/';
111 }
112 curl_setopt($this->curl, CURLOPT_URL, trim($this->host, '/') . '/' . trim($target));
113 if ($json) {
114 $this->headers[] = "Content-type: application/json";
115 }
116 if ($this->headers) {
117 curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->headers);
118 }
119 $this->headers = [];
120 $raw_response = curl_exec($this->curl);
121 $header_size = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE);
122 $this->reset();
123 $header = substr($raw_response, 0, $header_size);
124 $this->responseHeaders = array_filter(explode(PHP_EOL, $header), function ($line) {
125 return !preg_match("/^(HTTP|\r\n|Server|Date)/", $line) && trim($line);
126 });
127 $this->responseBody = substr($raw_response, $header_size);
128 }
129
130 /**
131 * Retorna o conteúdo da última requisição
132 * @param bool $json_decode
133 * @return mixed
134 *
135 */
136 public function getRespostaConteudo($json_decode = true)
137 {
138 if ($json_decode == true) {
139 return $this->decoded_resposta = json_decode($this->responseBody, true);
140 }
141 return $this->decoded_resposta = $this->responseBody;
142 }
143
144 /**
145 * Retorna o cabeçalho da ultima requisição
146 * @return array
147 */
148 public function getResposta_cabecalho()
149 {
150 return $this->responseHeaders;
151 }
152
153 //Iterator methods
154 public function current()
155 {
156 return current($this->decoded_resposta);
157 }
158
159 public function key()
160 {
161 return key($this->decoded_resposta);
162 }
163
164 public function next()
165 {
166 return next($this->decoded_resposta);
167 }
168
169 public function valid()
170 {
171 return is_array($this->decoded_resposta)
172 && (key($this->decoded_resposta) !== NULL);
173 }
174
175 //ArrayAcess methods
176 public function rewind()
177 {
178 $this->getRespostaConteudo(true);
179 return reset($this->responseBody);
180 }
181
182 public function offsetExists($chave)
183 {
184 $this->getRespostaConteudo(true);
185 return is_array($this->responseBody) ?
186 isset($this->responseBody[$chave]) : isset($this->responseBody->{$chave});
187 }
188
189 public function offsetGet($chave)
190 {
191 $this->decode_resposta();
192 if (!$this->offsetExists($chave)){
193 return NULL;
194 }
195 return is_array($this->decoded_resposta) ?
196 $this->decoded_resposta[$chave] : $this->decoded_resposta->{$chave};
197 }
198
199 public function offsetSet($chave, $valor)
200 {
201 throw new WebserviceClientException("Decoded resposta data is immutable.");
202 }
203
204 public function offsetUnset($chave)
205 {
206 throw new WebserviceClientException("Decoded resposta data is immutable.");
207 }
208
209 private function reset()
210 {
211 curl_reset($this->curl);
212 curl_setopt($this->curl, CURLOPT_HEADER, 1);
213 if ($this->token) {
214 curl_setopt($this->curl, CURLOPT_USERPWD, $this->token);
215 }
216 curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
217 if ($this->selfSigned) {
218 curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
219 }
220 }
221 }
-
Voltar para Página principal