Skip to content

Instantly share code, notes, and snippets.

@carloslopez1990
Last active August 29, 2015 14:21
Show Gist options
  • Save carloslopez1990/062dcfda9b169ab3c2a0 to your computer and use it in GitHub Desktop.
Save carloslopez1990/062dcfda9b169ab3c2a0 to your computer and use it in GitHub Desktop.
<?php
# [ IDENTIFICADOR DE LLAMADAS PARA NICARAGUA ]
# Creado por Carlos Ernesto López <celopez1990.blogspot.com>
# Contacto: [email protected] || facebook.com/c.ernest.1990
# Inspirado por la historia de Juan y Juanita
# http://celopez1990.blogspot.com/2015/04/juan-y-juanita.html (16 de abril de 2015)
class IdentificadorLlamadas
{
private $config;
private $numero;
private $error;
public $operadora;
private $resultado;
private $mh;
private $ch;
public $ch_counter;
public function __construct( $numero )
{
# Configuración de puntofacil.us
$this->config = [ 'payload_tpl' => '{"colector":"[COLECTOR]","servicio":"[SERVICIO]","tipoB":"250669A4-6F7C-E211-B93B-BC5FF42EE8D2",'.
'"idParm":"[NUM]","user":{"name":"C Ernesto López","_id":"547aac1656ed4c266d8aedae","username":'.
'"hi.oriondev","roles":["authenticated"],"provider":"facebook","email":"[email protected]","ipSource"'.
':"186.77.198.206","telephoneset":false}}',
'yota' => '2155D07F-BDC5-E311-A41A-BC5FF42EE8D2|B9159BF7-CF09-E311-8F13-BC5FF42EE8D2',
'colector_claro' => '965B972D-EF6B-E211-AC5B-BC5FF42EE8D2',
'opciones_claro' => [ 'casa_claro' => '2F0E010E-D009-E311-8F13-BC5FF42EE8D2',
'telefonia_fija' => 'A7CB9F41-3546-4F3A-BB60-3ED5D6E17036',
'claro_pospago' => 'D2BB76E2-CAAB-426B-883C-A7ED0B15C5A9',
'tv_cable' => '587D532F-DC04-4972-8376-F2C36786A409',
'internet_fijo' => '0C9D791E-A53B-4086-9389-62DE814F3B68',
'internet_movil' => 'B9159BF7-CF09-E311-8F13-BC5FF42EE8D2' ],
'movistar' => '4FED27A6-FD4A-4E52-88B0-405EDF90F110|D2BB76E2-CAAB-426B-883C-A7ED0B15C5A9' ];
# config prefijos operadoras
$this->config['prefijos_claro'] = explode(',', str_replace(' ', '', '222, 231, 550, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 581, 586, 820, 821, 822, 823, 833, 835, '.
'836, 840, 841, 842, 843, 844, 849, 850, 851, 852, 853, 854, 860, 861, 862, 863, 864, 865, 866, 869, 870, 871, '.
'872, 873, 874, 882, 883, 884, 885, 890, 891, 892, 893, 894'));
$this->config['prefijos_movistar'] = explode(',', str_replace(' ', '', '750, 770, 771, 772, 773, 774, 775, 778, 780, 781, 782, 783, 784, 785, 786, 787, 788, 789, 810, 811, 812, '.
'813, 814, 815, 816, 817, 818, 819, 824, 825, 826, 827, 828, 829, 832, 837, 838, 839, 845, 846, 847, 848, '.
'855, 856, 857, 858, 859, 867, 868, 875, 876, 877, 878, 879, 880, 881, 886, 887, 888, 889, 895, 896, 897, 898, 899'));
# Iniciar conexiones paralelas
$this->mh = curl_multi_init();
$this->ch_counter = -1;
# config misc
$this->resultado = null;
# validar número
return $this->setNumero( $numero );
}
public function setNumero( $numero )
{
$this->numero = (int) $numero;
if( strlen( $this->numero ) != 8 )
return false;
return true;
}
private function getOperadora()
{
$prefijo = substr( $this->numero, 0, 3 );
if( in_array($prefijo, $this->config['prefijos_claro']) )
$this->operadora = 'CLARO';
else if( in_array($prefijo, $this->config['prefijos_movistar']) )
$this->operadora = 'MOVISTAR';
else $this->operadora = 'INDEFINIDO';
}
private function prepPayload( $payload_cnf )
{
if( preg_match('/\|/', $payload_cnf) )
list( $colector, $servicio ) = explode('|', $payload_cnf);
else
{
$colector = $this->config['colector_claro'];
$servicio = $payload_cnf;
}
return str_replace( array( '[COLECTOR]', '[SERVICIO]', '[NUM]' ),
array( $colector, $servicio, $this->numero ),
$this->config['payload_tpl'] );
}
private function enviarPeticion( $payload )
{
if( $this->resultado != '' )
return;
$this->ch_counter++;
$this->ch[ $this->ch_counter ] = curl_init( 'https://www.puntofacil.us/getBills' );
curl_setopt( $this->ch[ $this->ch_counter ], CURLOPT_RETURNTRANSFER, true );
curl_setopt( $this->ch[ $this->ch_counter ], CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=UTF-8') );
curl_setopt( $this->ch[ $this->ch_counter ], CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $this->ch[ $this->ch_counter ], CURLOPT_POST, true );
curl_setopt( $this->ch[ $this->ch_counter ], CURLOPT_POSTFIELDS, $payload );
curl_multi_add_handle( $this->mh, $this->ch[ $this->ch_counter ] );
}
public function consultarClaro()
{
foreach( $this->config['opciones_claro'] as $req )
$this->enviarPeticion( $this->prepPayload( $req ) );
}
public function consultarYota()
{
$this->enviarPeticion( $this->prepPayload( $this->config['yota'] ) );
}
public function consultarMovistar()
{
$this->enviarPeticion( $this->prepPayload( $this->config['movistar'] ) );
}
public function identificar()
{
$this->getOperadora();
$this->consultarYota();
if( $this->operadora == 'MOVISTAR' )
$this->consultarMovistar();
else if( $this->operadora == 'CLARO' )
$this->consultarClaro();
else
{
$this->consultarMovistar();
$this->consultarClaro();
}
return $this->getResultado();
}
public function getResultado()
{
$active = null;
do {
$mrc = curl_multi_exec($this->mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK)
{
if (curl_multi_select($this->mh) == -1)
usleep(100);
do {
$mrc = curl_multi_exec($this->mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
for( $x = 0; $x < count( $this->ch ); $x++ )
{
$info = curl_multi_getcontent( $this->ch[ $x ] );
curl_multi_remove_handle( $this->mh, $this->ch[ $x ] );
$info = json_decode( $info );
$info = @$info[0]->nombres;
$this->resultado = $info;
if( $this->resultado != '' )
break;
}
curl_multi_close( $this->mh );
$this->ch_counter = -1;
if( $this->resultado == '' )
return 'NO_ENCONTRADO';
return str_replace("ñ", "Ñ", strtoupper( $this->resultado ));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment