IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    获取手机归属地(memcache与curl结合的使用)

    萧逸先生发表于 2017-02-23 13:01:12
    love 0

    curl与memcache结合实现手机归属地查询

    本例主要使用了360的api实现了归属地的查询,将查询结果缓冲到memcache,通过memcache获取:

    1:简单封装了下curl 实现curl方法的请求:

    常用的请求还有:file_put_contents ,socket,curl

     
        /**
         * [curlRequest 请求方法]
         * @param  [type]  $url    [请求地址]
         * @param  boolean $https  [请求协议 支持https 与http]
         * @param  string  $method [请求方式] 支持post与get]
         * @param  [type]  $data   [请求数据,post]
         * @return [type]          [返回数据结果]
         */
    
        function curlRequest($url, $https = true, $method = 'get', $data = null)
            {
                //curl初始化
                $ch = curl_init($url);
                //直接输出到页面
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                if ($https === true) {
                    //跳过https验证
                    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                }
                //支持post请求
                if ($method === 'post') {
                    curl_setopt($ch, CURLOPT_POST, true);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                }
                //发送请求
                if (($c = curl_exec($ch)) === false) {
                    return false;
                }
                //关闭连接
                curl_close($ch);
                return $c;
    }
    

    2:数据的请求 实现手机号的查询 请求api地址实现数据的查询 也可以通过mysql处理

    /**
     * [getAreaByTel description]
     * @param  [int] $tel [手机号码]
     * @return [json | xml]      [返回响应格式的数据]
     */
    function getAreaByTel($tel=18234853823,$type='json')
    {
        if(preg_match('/0?(13|14|15|18)[0-9]{9}/',$tel)){
          //1.url
          $url="http://cx.shouji.360.cn/phonearea.php?number=".substr($tel,0,7);
          //2.发送请求
          $data=curlRequest($url,false);
        }else{
            $data=array(
                'status'=>0,
                'msg'=>'手机号格式错误'
                );
            return json_encode($data);
        }
        $data=getMemcacheTel(json_decode($data)->data);
        switch ($type) {
            case 'xml':
                $str="<?xml version='1.0' encoding='utf8'?>";
                $str.='<province>'.$data->province.'</province>';
                $str.='<city>'.$data->city.'</city>';
                $str.='<sp>'.$data->sp.'</sp>';
                return  $str;
            default:
                return json_encode($data);
                break;
        }
    }
    

    3:memcache的连接与存储 实现手机信息的存储

    /**
     * [getMemcacheTel 缓冲归属地信息]
     * @param  [type] $data [手机的归属地]
     * @return [type]       [缓冲归属地信息]
     */
    function getMemcacheTel($data)
    {
        //解决脚本内存占用问题  默认限制大小 防止服务器受到攻击 服务器崩溃
        ini_set('memory_limit','500M');
        //设置脚本临时执行时间 0表示不超时
        set_time_limit(0);
        $m = new Memcache();
        $m->connect('localhost',11211);
        if($m->get('phone')===false){
            $m->set('phone',$data,0,0);
            return $m->get('phone');
        }else{
            return $m->get('phone');
        }
    }
    


沪ICP备19023445号-2号
友情链接