⬆️ ⬇️

We recognize the operator and the region of the mobile phone

image

Like walking on the Internet, I came across an interesting link - Codes of mobile operators . And I really wanted to have such a database locally.

Under the dump there is a mysql database dump, php code for its use and a script parser for updating.



First you need to download the database .

Then you can get data from the database like this:



<?php

$config[ 'mysql_host' ] = 'localhost' ;

$config[ 'mysql_user' ] = 'root' ;

$config[ 'mysql_password' ] = '' ;

$config[ 'mysql_base' ] = 'smsprice' ;



define( "PREFIX" , "mtt_" );



$mysql = new mysqli($config[ 'mysql_host' ],$config[ 'mysql_user' ],$config[ 'mysql_password' ],$config[ 'mysql_base' ]);

$mysql->query( "SET NAMES 'cp1251'" );



$number = '+7(910)89-480-23' ;



$number = preg_replace( '/[^0-9]+/' , '' ,$number);



if (strlen($number) == 11) {

$number = substr($number,1);

}



if (strlen($number) == 10) {

$def = substr($number,0,3);

$code = substr($number,3);

$stmt = $mysql->stmt_init();

$stmt->prepare(

'SELECT regions.name AS region, operators.name AS operator ' .

'FROM `' .PREFIX. 'codes` AS codes ' .

'INNER JOIN `' .PREFIX. 'regions` AS regions ON regions.id = codes.region ' .

'INNER JOIN `' .PREFIX. 'operators` AS operators ON operators.id = codes.operator ' .

'WHERE `def` = ? ' .

'AND ? < `to` ' .

'AND ? > `from`'

);

$stmt->bind_param( "iii" , $def, $code, $code);

$stmt->execute();

$stmt->bind_result($region, $ operator );

while ($stmt->fetch()) {

echo " — " .htmlspecialchars($region). "<br/>\r\n" ;

echo " — " .htmlspecialchars($ operator ). "<br/>\r\n" ;

}

$stmt->close();

} else {

echo ': ' ;

}

?>




* This source code was highlighted with Source Code Highlighter .


I'm not sure about the speed of requests and the structure of the database, so if someone advises how to do it right, I will be very grateful.

If you do not want to suffer with the filling of the base and php, then you can see how it works here .



UPD:

At the request of the workers, the parser code for updating the database with mtt:

')

<?php

set_time_limit(0);



$config[ 'mysql_host' ] = 'localhost' ;

$config[ 'mysql_user' ] = 'root' ;

$config[ 'mysql_password' ] = '' ;

$config[ 'mysql_base' ] = 'smsprice' ;



define( "PREFIX" , "mtt_" );



$mysql = new mysqli($config[ 'mysql_host' ],$config[ 'mysql_user' ],$config[ 'mysql_password' ],$config[ 'mysql_base' ]);

$mysql->query( "SET NAMES 'cp1251'" );



$regxp = '#' .

'<tr>\s*' .

'<td abbr=".*">(.*)</td>\s*' .

'<td abbr=".*">(.*)</td>\s*' .

'<td abbr="">(.*)</td>\s*' .

'<td abbr="(.*)" align="right">\s*' .

'.*' .

'<td abbr="(.*)"><nobr>.*</nobr></td>\s*' .

'</tr>' .

'#isU' ;



$page = file_get_contents( 'http://mtt.ru/info/def/index.wbp?def=&number=®ion=&standard=&date=&operator=' );

preg_match_all($regxp,$page,$result);



$mysql->query( 'TRUNCATE TABLE `' .PREFIX. 'codes`' );



for ($i=0,$l=count($result[0]);$i<$l;$i++) {

$ operator = $result[1][$i];

$region = $result[2][$i];

$def = $result[3][$i];

list($range_from,$range_to) = explode( "-" ,$result[4][$i]);

$range_from = substr($range_from,3);

$range_to = substr($range_to,3);

$date = str_replace( "." , "-" ,$result[5][$i]);



$stmt = $mysql->stmt_init();

$stmt->prepare( "SELECT id FROM " .PREFIX. "operators WHERE name=?" );

$stmt->bind_param( "s" , $ operator );

$stmt->execute();

$stmt->store_result();



if ($stmt->num_rows > 0) {

$stmt->bind_result($ operator );

$stmt->fetch();

$stmt->close();

} else {

$stmt->close();

$stmt = $mysql->stmt_init();

$stmt->prepare( "INSERT INTO " .PREFIX. "operators VALUES ('', ?)" );

$stmt->bind_param( "s" , $ operator );

$stmt->execute();

$ operator = $stmt->insert_id;

$stmt->close();

}



$stmt = $mysql->stmt_init();

$stmt->prepare( "SELECT id FROM " .PREFIX. "regions WHERE name=?" );

$stmt->bind_param( "s" , $region);

$stmt->execute();

$stmt->store_result();



if ($stmt->num_rows > 0) {

$stmt->bind_result($region);

$stmt->fetch();

$stmt->close();

} else {

$stmt->close();

$stmt = $mysql->stmt_init();

$stmt->prepare( "INSERT INTO " .PREFIX. "regions VALUES ('', ?)" );

$stmt->bind_param( "s" , $region);

$stmt->execute();

$region = $stmt->insert_id;

$stmt->close();

}



$stmt = $mysql->stmt_init();

$stmt->prepare( "INSERT INTO " .PREFIX. "codes VALUES ('', ?, ?, ?, ?, ?, ?)" );

$stmt->bind_param( "iiiiis" , $ operator , $region, $def, $range_from, $range_to, $date);

$stmt->execute();

$stmt->close();

}

?>




* This source code was highlighted with Source Code Highlighter .

Source: https://habr.com/ru/post/45865/



All Articles