The site in question (consultanumero.info/consulta
) implemented a Recaptcha, which makes the code below not currently working.
You can just make one foreach
of the kind`
$numeros = [
'11999999999',
'22988888888',
'21912345678'
];
Then do:
foreach($numeros as $numero){
get_operadora($numero);
}
But there are several that can worsen the performance, see this:
if (preg_match("/(oi)/", $data)) {
$resultado = "OI";
}
if (preg_match("/(vivo)/", $data)) {
$resultado = "VIVO";
}
if (preg_match("/(tim)/", $data)) {
$resultado = "TIM";
}
if (preg_match("/(claro)/", $data)) {
$resultado = "CLARO";
}
if (preg_match("/(nextel)/", $data)) {
$resultado = "NEXTEL";
}
return trim($resultado);
curl_close($ch);
"Mistakes":
No phone has two operators, so it’s either OI
or is VIVO
, your code doesn’t care about this. If it finds OI
he will still seek to VIVO
, for TIM
, for CLARO
... The use of elseif
would reduce this.
The curl_close()
will never be used, the return
is before him.
A better option would be to use the multi_curl
and use the preg_match
, thus removing the if
and Curl will run faster:
function get_operadora(array $telefones){
$curlIndividual = [];
$operadora = [];
$curlTodos = curl_multi_init();
foreach($telefones as $telefone){
$curlIndividual[$telefone] = curl_init('https://consultanumero.info/consulta');
curl_setopt_array($curlIndividual[$telefone], [
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; rv:32.0) Gecko/20100101 Firefox/32.0',
CURLOPT_SSL_VERIFYPEER => 1,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS | CURLPROTO_HTTP,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => 'tel='.$telefone,
CURLOPT_RETURNTRANSFER => 1
]);
curl_multi_add_handle($curlTodos, $curlIndividual[$telefone]);
}
$Executando = 1;
while($Executando> 0){
curl_multi_exec($curlTodos, $Executando);
curl_multi_select($curlTodos);
}
foreach($curlIndividual as $telefone => $curl){
$resultado = curl_multi_getcontent($curl);
if(preg_match('/<img src="(.*?)" alt="(.*?)" title="(.*?)" \/>/', $resultado, $matches)) {
$operadora[$telefone] = $matches[2];
}
}
return $operadora;
}
Changes:
Only accepted array
.
Checks SSL with the certificates you trust due to CURLOPT_SSL_VERIFYPEER
, which is recommended and is standard PHP 7.1.
Only uses HTTP/HTTPS, defined in CURLOPT_PROTOCOLS
.
Utilizes the multi_curl
, defined in curl_multi_exec
.
Get the operator based on <img src="(.*?)" alt="(.*?)" title="(.*?)" \/>
, without any if
.
Utilizing:
$numeros = [
'11999999999',
'22988888888',
'21999991234'
];
get_operadora($numeros);
Return:
array(3) {
["11999999999"]=>
string(4) "Vivo"
["22988888888"]=>
string(2) "Oi"
["21999991234"]=>
string(4) "Vivo"
}
Tested in PHP 7.1, older versions may have incompatibility.
Heed:
Consider this as an example, the ideal would be to break it into several functions and check that all variables are being set correctly.
In case I will use html form for $numeros = [ '11999999999', '22988888888', '21999991234' ]; because I need to assemble a large list, help me to assemble the code?
– Leonardo Ribeiro
Ask more clearly, just create a
array
with the numbers, there are several questions about it here.– Inkeliz
In this case, I wanted to put a list of numbers in a textarea and it will print the results of the operators understands so that I can separate everything
– Leonardo Ribeiro
See by
explode
, http://answall.com/a/70442/15089– Inkeliz
got it, I’ll make an example here, you help me with the whole code after I assemble here ?
– Leonardo Ribeiro
look at > https://pastebin.com/EGeC0zQ3
– Leonardo Ribeiro
Define the
get_operadora()
afterwardget_operadora(explode("\n", $_GET['numeros']));
. Thesplit
is obsolete and not supported in PHP 7+.– Inkeliz