✏️ 正在编辑: Ibge.php
路径:
/srv/systems_dir/koruspay/application/services/Ibge.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php class Service_Ibge { public function getCodigoCidadeIbge($uf, $cidade) { $url = "https://servicodados.ibge.gov.br/api/v1/localidades/estados/{$uf}/municipios?view=nivelado"; $cidades = (array)$this->sendRequest($url, "GET"); $codigoMunicipio = null; if (empty($cidades)) { return null; } foreach ($cidades as $c) { if (strtolower($c->{'municipio-nome'}) == strtolower($cidade)) { $codigoMunicipio = $c->{'municipio-id'}; break; } } return $codigoMunicipio; } protected function sendRequest($url, $method, $params = '', $headers = []) { $curl = curl_init(); $options = [ CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => $method, CURLOPT_POSTFIELDS => $params, ]; if (!empty($headers)) { $options[CURLOPT_HTTPHEADER] = $headers; } curl_setopt_array($curl, $options); $response = curl_exec($curl); curl_close($curl); return json_decode($response); } public function getDadosCidadePorCep($cep) { $headers = [ "x-api-key: 4ca5eee3-6038-43b1-bfb6-00c65b853c75", ]; return $this->sendRequest("https://api.plugnotas.com.br/cep/{$cep}", "GET", '', $headers); } }
💾 保存文件
← 返回文件管理器