Question:
I started a query to find out which cell phone operator is pulling from a website. However, I can only do one at a time. I would like help so that I could paste a list and it would send requests and print the result separately.
<?php
$fone = $_POST['tel_fone'];
$fone = preg_replace("/[^0-9]/", "", $fone); function get_operadora($fone){ $url = "http://consultanumero.info/consulta";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:32.0) Gecko/20100101 Firefox/32.0");
curl_setopt ($ch, CURLOPT_REFERER, 'http://google.com.br/');
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "tel=$fone");
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec ($ch); 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); }
$operadora = get_operadora($fone);
?>
Answer:
The site in question (
consultanumero.info/consulta
) has implemented a ReCaptcha, which makes the code below not work currently.
You can just do a foreach
of type`
$numeros = [
'11999999999',
'22988888888',
'21912345678'
];
Then do:
foreach($numeros as $numero){
get_operadora($numero);
}
But there are several that can worsen 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);
"Errors":
-
No phone has two operators, so it's either
OI
orVIVO
, your code doesn't care about that. If it findsOI
it will still search forVIVO
, forTIM
, forCLARO
… Usingelseif
would reduce this. -
curl_close()
will never be used,return
is before it.
A better option would be to use multi_curl
and use preg_match
, so 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 accepts
array
. -
Verifies SSL with certificates you trust, due to
CURLOPT_SSL_VERIFYPEER
, which is recommended and defaults to PHP 7.1 . -
It only uses HTTP/HTTPS, defined in
CURLOPT_PROTOCOLS
. -
Uses
multi_curl
, defined incurl_multi_exec
. -
Gets the operator based on the
<img src="(.*?)" alt="(.*?)" title="(.*?)" \/>
, without anyif
.
Use:
$numeros = [
'11999999999',
'22988888888',
'21999991234'
];
get_operadora($numeros);
Return:
array(3) {
["11999999999"]=>
string(4) "Vivo"
["22988888888"]=>
string(2) "Oi"
["21999991234"]=>
string(4) "Vivo"
}
Tested on PHP 7.1, old versions may have incompatibility.
Heads up:
Consider this as an example, the ideal would be to break this into several functions and check that all the variables are being defined correctly.