龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php 快速定位手机号码归属地

时间:2014-12-01 03:01来源:网络整理 作者:网络 点击:
分享到:
快速定位手机号码归属地 在WAMP环境下需要配置下curl扩展,通过和前台ajax进行数据交互的话可以很好的实现注册时手机号码归属地的验证[代码片段(61行)]

在WAMP环境下需要配置下curl扩展,通过和前台ajax进行数据交互的话可以很好的实现注册时手机号码归属地的验证

<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 5                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.php.net/license/3_0.txt.                                  |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
//

$mobile_home = new mobile_home();
$mobile_home->set_mobile(18606240180);
$data = $mobile_home->showji();//data是一个数组,可以获取到你想要的信息
//print_r($data);
/**
 * 
 * 手机归属地查询 
 * 
 * 通过curl扩展来实现查询手机号码归属地
 */
//定义一个class
class mobile_home {
    public $mobile;
    public function set_mobile($mobile) {
        $this->mobile = $mobile;
        $this->checklen();
    }
    //检测手机位数
    public function checklen() {
        if (strlen($this->mobile) != "11") {
            die("手机号码有误");
        }
    }
    //调用官方接口
    public function showji() {
        $data = $this->get('http://api.showji.com/Locating/www.showji.com.aspx?m=' . $this->mobile . '&output=json', '', "http://api.showji.com");
        if (is_array($data)) {
            return $data;
        }
    }
    //通过curl扩展获取数据信息
    private function get($urll, $cookie = '', $referer = '') {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $urll);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_REFERER, $referer);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        $content = curl_exec($ch);
        curl_close($ch);
        return json_decode($content, true);
    }
}
?>
//该片段来自于http://outofmemory.cn
精彩图集

赞助商链接