mobile operator consultation

3

I started a query to find out which cell phone carrier is pulling from a website. However, I can only do one at a time. I would like help so I could paste a list and it would be sending in requisitions 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.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);
?>
    
asked by anonymous 31.03.2017 / 14:02

1 answer

3

You can simply make 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 carriers, so either it's OI or it's VIVO , your code does not connect to it. If it finds OI it will still fetch VIVO , TIM , CLARO ... elseif would reduce this.

  • The curl_close() will never be used, return is before it.

  • A better option would be to use multi_curl and use preg_match , thus removing 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('http://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 accept array .

    • Only use HTTP / HTTPS, set to CURLOPT_SSL_VERIFYPEER .

    • Uses CURLOPT_PROTOCOLS , set to multi_curl .

    • Gets the operator based on curl_multi_exec , without any <img src="(.*?)" alt="(.*?)" title="(.*?)" \/> .

    Usage:

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

    Attention:

    Consider this as an example, ideally it would break this down into various functions and check if all variables are being defined correctly.

        
    31.03.2017 / 15:05