首先我们介绍使用自己的数据库查询多个手机号码,那还是建议你拥有一个自己的的手机号码数据库。正常情况下,只是满足一般查询的话,你不需要去购买专业版的手机号码数据库,增加无谓成本。我免费为你提供一个ACCESS数据库,包含17万多条数据,常用的130-139、150-159以及180-189开头手机号码段都在其中,你可以借助数据库工具轻松地将它转换成MYSQL或其它版本数据库
最新手机号码数据库下载地址: http://xiazai.jb51.net/201209/yuanma/phone-number-database-jb51.rar
PHP+MYSQL手机号码归属地查询实现方法
通过上面的介绍,我们已经有了自己的MYSQL数据表。这个表结构很简单:ID(序号),code(区号),num(手机号码段),cardtype(手机卡类型),city(手机号码归属地)。注意,这个表存储数据量很大,应当根据你的sql查询语句,建立合适的索引字段,以提高查询效率。
1)获取手机号码归属地,我们只需要通过判断手机号码段归属地即可。主要通过以下函数实现,其中GetAlabNum、cn_substr、str_replace都是字符串操作函数,$dsql是数据库操作类。下面的代码是在canphp2.0的indexMod文件呢的代码
<?php
class indexMod extends commonMod {
public function index() {
$this->assign ( "title", "首页" );
$this->assign ( "msg", "欢迎使用cp2.0" );
$this->display ();
}
public function show() {
$info = array (
'title' => '文章标题',
'content' => '文章内容'
);
$this->assign ( "title", "显示页面" );
$this->assign ( "info", $info );
$this->display ();
}
public function showdb0() {
$info = array (
'title' => '文章标题',
'content' => '文章内容'
);
$info = $this->model->table ( 'info' )->where ( "infoid=1" )->find ();
print_r ( $info );
$this->assign ( "title", "显示页面" );
$this->assign ( "info", $info );
$this->display ();
}
public function showdb() {
$info = array (
'title' => '文章标题',
'content' => '文章内容'
);
$aa = $this->model->table ( 'info' )->where ( "infoid=1" )->find ();
$tel = "13580531231";
$tel = substr ( $tel, 0, 7 );
$row = $this->model->table ( 'mobiletype' )->where ( "num='" . $tel . "'" )->find ();
print_r ( $row );
$this->assign ( "title", "显示页面" );
$this->assign ( "info", $info );
$this->display ();
}
}
api实现方法,这里不需要自己的数据库但有限制了
主要使用curl实现,需要开启php对curl的支持。
<?php
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 '手机号码格式错误!';
} else {
echo $data;
}
}
?>
<form action="mobile.php" method="get">
手机号码: <input type="text" name="number" /> <input type="submit" value="提交" />
</form>
最新手机号码数据库下载地址: http://xiazai.jb51.net/201209/yuanma/phone-number-database-jb51.rar
PHP+MYSQL手机号码归属地查询实现方法
通过上面的介绍,我们已经有了自己的MYSQL数据表。这个表结构很简单:ID(序号),code(区号),num(手机号码段),cardtype(手机卡类型),city(手机号码归属地)。注意,这个表存储数据量很大,应当根据你的sql查询语句,建立合适的索引字段,以提高查询效率。
1)获取手机号码归属地,我们只需要通过判断手机号码段归属地即可。主要通过以下函数实现,其中GetAlabNum、cn_substr、str_replace都是字符串操作函数,$dsql是数据库操作类。下面的代码是在canphp2.0的indexMod文件呢的代码
复制代码 代码如下:
<?php
class indexMod extends commonMod {
public function index() {
$this->assign ( "title", "首页" );
$this->assign ( "msg", "欢迎使用cp2.0" );
$this->display ();
}
public function show() {
$info = array (
'title' => '文章标题',
'content' => '文章内容'
);
$this->assign ( "title", "显示页面" );
$this->assign ( "info", $info );
$this->display ();
}
public function showdb0() {
$info = array (
'title' => '文章标题',
'content' => '文章内容'
);
$info = $this->model->table ( 'info' )->where ( "infoid=1" )->find ();
print_r ( $info );
$this->assign ( "title", "显示页面" );
$this->assign ( "info", $info );
$this->display ();
}
public function showdb() {
$info = array (
'title' => '文章标题',
'content' => '文章内容'
);
$aa = $this->model->table ( 'info' )->where ( "infoid=1" )->find ();
$tel = "13580531231";
$tel = substr ( $tel, 0, 7 );
$row = $this->model->table ( 'mobiletype' )->where ( "num='" . $tel . "'" )->find ();
print_r ( $row );
$this->assign ( "title", "显示页面" );
$this->assign ( "info", $info );
$this->display ();
}
}
api实现方法,这里不需要自己的数据库但有限制了
主要使用curl实现,需要开启php对curl的支持。
复制代码 代码如下:
<?php
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 '手机号码格式错误!';
} else {
echo $data;
}
}
?>
<form action="mobile.php" method="get">
手机号码: <input type="text" name="number" /> <input type="submit" value="提交" />
</form>