1
I’m making an app that has phone numbers. To make life easier for users, I would like to return the DDD that does not appear in some numbers.
Does anyone know any API, or anything like that to return the DDD according to the city.
1
I’m making an app that has phone numbers. To make life easier for users, I would like to return the DDD that does not appear in some numbers.
Does anyone know any API, or anything like that to return the DDD according to the city.
5
There is the http://ddd.pricez.com.br and can use with PHP or jQuery (or any HTTP query medium)
var siglaEstado = "am";
var url = "https://ddd.pricez.com.br/estados/" + siglaEstado.toLowerCase();
$.getJSON(url).done(function (data) {
console.log(data);
}).fail(function (xhr, codigo, c) {
console.log( "error:", xhr.readyState, codigo);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Then in the .done
just filter what you want, for the sake of having accents it might be interesting to implement String.normalize
, as in the example in: /a/148229/3635
for example:
function removeAcentos(s) {
return s.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
}
function consultar(siglaEstado, resposta) {
var url = "https://ddd.pricez.com.br/estados/" + siglaEstado.toLowerCase();
$.getJSON(url).done(function (data) {
resposta(data);
}).fail(function (xhr, codigo, c) {
resposta(false);
});
}
(function () {
var consultando = false;
var qualDDD = document.getElementById("qual-ddd");
var consultaDDD = document.getElementById("consulta-ddd");
consultaDDD.addEventListener("submit", function (e) {
e.preventDefault(); //cancela o form normal
consultando = true;//Bloqueia novas consultas até terminar
qualDDD.textContent = "Consultando...";
var estado = consultaDDD.querySelector("[name=estado]").value;
var cidade = removeAcentos( consultaDDD.querySelector("[name=cidade]").value );
consultar(estado, function (dados) {
if (!dados) {
qualDDD.textContent = "Falha na requisição";
return;
}
if (!dados.count) {
qualDDD.textContent = "estado não encontrado";
return;
}
for (var i = 0, j = dados.payload.length; i < j; i++) {
if (cidade === removeAcentos(dados.payload[i].cidade)) {
qualDDD.textContent = "O DDD é " + dados.payload[i].ddd;
}
}
consultando = false;
});
});
})();
#qual-ddd {
font-size: 19pt;
padding: 15px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="qual-ddd"></div>
<form id="consulta-ddd">
<select name="estado">
<option value="am">Amazonas</option>
<option value="sp" selected>São Paulo</option>
</select>
<input name="cidade" value="São Paulo">
<button>Consultar</button>
</form>
To get the data with PHP you can use curl
or file_get_contents
, in the example I will apply Curl, because file_get_contents requires certain settings how many to the security certificates and many servers disable allow_url_open in php.ini.
In PHP it should look like this more or less:
<?php
if (isset($_POST['cidade']{0}, $_POST['estado']{0})) {
$estado = $_POST['estado'];
$cidade = $_POST['cidade'];
//Codifica para url os dados
$estado = urlencode($estado);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://ddd.pricez.com.br/estados/' . $estado);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Accept:application/json' ));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
if($data === false) {
echo 'Erro ao executar o CURL: ' . curl_error($ch);
} else {
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpcode !== 200) {
echo 'Erro ao requisitar o servidor';
$data = null;
}
}
curl_close($ch);
function simplificaRemoveAcentos($texto)
{
$encode = mb_detect_encoding($texto, mb_detect_order(), true);
$texto = 'ASCII' === $encode ? $texto: iconv($encode, 'ASCII//TRANSLIT//IGNORE', $texto);
return trim(strtolower(preg_replace('#[^a-z0-9\s]#i', '', $texto)));
}
if ($data) {
$ddd = false;
$payload = json_decode($data)->payload;
$cidade = simplificaRemoveAcentos($cidade);
foreach ($payload as $value) {
if (simplificaRemoveAcentos($value->cidade) === $cidade) {
$ddd = $value->ddd;
}
}
if ($ddd) {
echo 'O DDD é ', $ddd;
} else {
echo 'Cidade não encontrada';
}
}
}
?>
<form method="POST" action="">
<select name="estado">
<option value="am">Amazonas</option>
<option value="sp" selected>São Paulo</option>
</select>
<input name="cidade" value="São Paulo">
<button>Consultar</button>
</form>
Very good, thank you...
-4
Use that:
<select id="uf" name="uf">
<option value='' disabled selected>DDD</option>
<option value='11'>11</option>
<option value='12'>12</option>
<option value='13'>13</option>
<option value='14'>14</option>
<option value='15'>15</option>
<option value='16'>16</option>
<option value='17'>17</option>
<option value='18'>18</option>
<option value='19'>19</option>
<option value='21'>21</option>
<option value='22'>22</option>
<option value='24'>24</option>
<option value='27'>27</option>
<option value='28'>28</option>
<option value='31'>31</option>
<option value='32'>32</option>
<option value='33'>33</option>
<option value='34'>34</option>
<option value='35'>35</option>
<option value='37'>37</option>
<option value='38'>38</option>
<option value='41'>41</option>
<option value='42'>42</option>
<option value='43'>43</option>
<option value='44'>44</option>
<option value='45'>45</option>
<option value='46'>46</option>
<option value='47'>47</option>
<option value='48'>48</option>
<option value='49'>49</option>
<option value='51'>51</option>
<option value='53'>53</option>
<option value='54'>54</option>
<option value='55'>55</option>
<option value='61'>61</option>
<option value='62'>62</option>
<option value='63'>63</option>
<option value='64'>64</option>
<option value='65'>65</option>
<option value='66'>66</option>
<option value='67'>67</option>
<option value='68'>68</option>
<option value='69'>69</option>
<option value='71'>71</option>
<option value='73'>73</option>
<option value='74'>74</option>
<option value='75'>75</option>
<option value='77'>77</option>
<option value='79'>79</option>
<option value='81'>81</option>
<option value='82'>82</option>
<option value='83'>83</option>
<option value='84'>84</option>
<option value='85'>85</option>
<option value='86'>86</option>
<option value='87'>87</option>
<option value='88'>88</option>
<option value='89'>89</option>
<option value='91'>91</option>
<option value='92'>92</option>
<option value='93'>93</option>
<option value='94'>94</option>
<option value='95'>95</option>
<option value='96'>96</option>
<option value='97'>97</option>
<option value='98'>98</option>
<option value='99'>99</option>
</select>
Using the zip code: 08150-300 as an example, the return of the query is:
{"payload": {
"bairro": "Jardim Robru",
"cep": "08150300",
"cidade": "S\u00e3o Paulo",
"cidade_info": {
"area_km2": "1521,11",
"codigo_ibge": "3550308"
},
"ddd": 11,
"estado": "SP",
"estado_info": {
"area_km2": "248.221,996",
"codigo_ibge": "35",
"nome": "S\u00e3o Paulo"
},
"logradouro": "Rua Severino Jos\u00e9 Fernandes"
}
}
I make the call at the event Blur ZIP field
$("#cep").on("blur",function(e){
//Limpo os campos...
$("#endereco, #bairro, #cidade, #uf, #ddd").val("");
var cepConsulta = $(this).val().replace(/\D/g, '');
if (cepConsulta != "") {
showModal("Aguarde<br>Consultando endereço...");
$.getJSON("https://ddd.pricez.com.br/cep/"+cepConsulta).done(function (data) {
if(data.payload.bairro === undefined){
alert("Não foi possível localizar o endereço\nVerifique o CEP digitado");
$("#cep").focus();
return false;
}
//Preencho os campos
$("#endereco").val(data.payload.logradouro);
$("#bairro").val(data.payload.bairro);
$("#cidade").val(data.payload.cidade);
$("#uf").val(data.payload.estado);
//aqui vai selecionar o DDD do select anterior
$("#ddd").val(data.payload.ddd);
}).fail(function (xhr, codigo, c) {
console.log( "error:", xhr.readyState, codigo);
}).always(function() {
closeModal();
});
}
And what would this API look like? And how to select the DDD according to the CEP, with this code you suggested?
I improved the above code friend, with an example of use
Browser other questions tagged php javascript jquery api
You are not signed in. Login or sign up in order to post.
API no, but it is a thing that can be easily mounted in the hand. You have websites which list all cities by ddd. From there, it’s just soping and pasting to a table and implementing a Json object, a set of lists, dictionaries, or the data structure you prefer. If you can publish this as an independent library it helps the whole community.
– Cochise