This article introduces how to implement mobile phone number attribution. we can use the api interface and mysql + php for example. if you need it, let's first look at how to use our own database to query multiple mobile phone numbers, we recommend that you have your own mobile phone number database. Under normal circumstances, you do not need to purchase the mobile phone number database of the Professional edition to increase unnecessary costs. I will provide you with an ACCESS database for free, containing more than 0.17 million pieces of data. the commonly used 130-139, 150-159, and mobile phone number segments starting with 180-189 are all in it, you can use the database tool to easily convert it to MYSQL or another version of the database.
Latest mobile phone number database: http://xiazai.jb51.net/201209/yuanma/phone-number-database-jb51.rar
PHP + MYSQL mobile phone number attribution query implementation method
Through the above introduction, we already have our own MYSQL data table. This table has a simple structure: ID (serial number), code (area code), num (mobile phone number segment), cardtype (mobile phone card type), and city (mobile phone number attribution ). Note: This table stores a large amount of data. you should create appropriate index fields based on your SQL query statements to improve query efficiency.
1) to obtain the location of the phone number, we only need to determine the location of the phone number segment. The following functions are mainly implemented: GetAlabNum, cn_substr, and str_replace are all string operation functions, and $ dsql is a database operation class.
The code is as follows:
Function GetTelphone ($ tel)
{
Global $ city, $ dsql;
If (isset ($ tel) $ tel = GetAlabNum (trim ($ tel); // The GetAlabNum function is used to replace a full-width number and convert an invalid mobile phone number to a number; trim unnecessary spaces.
Else return false;
If (strlen ($ tel) <7) return false;
$ Tel = cn_substr ($ tel, 11); // truncates 11 characters to prevent multiple phone numbers.
// If (! Is_numeric ($ tel) return false;
If (cn_substr ($ tel, 1) = "0") // determines whether the phone number starts with 0. in this case, the landline number may start with 0.
{
If (cn_substr ($ tel, 2) = "01" | cn_substr ($ tel, 2) = "02") $ tel = cn_substr ($ tel, 3 ); // Three-digit area code
Else $ tel = cn_substr ($ tel, 4 );
$ Row = $ dsql-> GetOne ("Select code, city as dd from '#@__ tel' where code = '$ tel' group by code ");
}
Else
{
$ Tel = cn_substr ($ tel, 7 );
$ Row = $ dsql-> GetOne ("Select num, city as dd from '#@__ tel' where num = '$ tel '");
}
$ City = $ row ['DD'];
If ($ city)
{
$ City = str_replace ("province", "-", $ city );
$ City = str_replace ("city", "", $ city );
$ City ="
[". $ City."] ";
Return $ city;
}
}
Api implementation method. you do not need to use your own database, but there are restrictions.
This is mainly implemented using curl. you need to enable php's support for curl.
The code is as follows:
Header ("Content-Type: text/html; charset = utf-8 ″);
If (isset ($ _ GET ['Number']) {
$ Url = 'http: // webservice.webxml.com.cn/webservices/mobilecodews.asmx/getmobilecodeinfo ';
$ Number = $ _ GET ['Number'];
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_POST, true );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, "mobileCode = {$ number} & userId = ");
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
$ Data = curl_exec ($ ch );
Curl_close ($ ch );
$ Data = simplexml_load_string ($ data );
If (strpos ($ data, 'http ://')){
Echo 'mobile phone number format error! ';
} Else {
Echo $ data;
}
}
?>
It is much slower to query php mysql mobile phone numbers. after all, you need to use the third method of data.