consultation of mobile operators

Asked

Viewed 2,187 times

4

I started a query to know which mobile operator is pulling from a site. But I can only do it once. I would like help so that I could paste a list and it was sending requests and printing the separate result.

<?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/'); 
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);
?>

1 answer

3


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":

  1. 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.

  2. 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?

  • Ask more clearly, just create a array with the numbers, there are several questions about it here.

  • 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

  • See by explode, http://answall.com/a/70442/15089

  • got it, I’ll make an example here, you help me with the whole code after I assemble here ?

  • look at > https://pastebin.com/EGeC0zQ3

  • Define the get_operadora() afterward get_operadora(explode("\n", $_GET['numeros']));. The split is obsolete and not supported in PHP 7+.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.