php get/post 请求(可用于请求api)获取手机号码归属地

        //获取手机归属地
        header("content-type:text/html;charset=utf-8");             //设置utf8
        
        $tel = "15251507015";                                       //手机号
        $url = "http://sj.apidata.cn/?mobile=".$tel;                //api接口地址
        $res = $this->request_get($url);
        $res_arr = json_decode($res,true);

        if($res_arr['status']=='1'){                                //如果成功获取数据
            $area['province'] = $res_arr['data']['province'];
            $area['city'] = $res_arr['data']['city'];

        }
        var_dump($area);

 

    /**
     * 发送post请求
     * @param string $url
     * @param string $param
     * @return bool|mixed
     */
    function request_post($url = '', $param = '')
    {
        if (empty($url) || empty($param)) {
            return false;
        }
        $postUrl = $url;
        $curlPost = $param;
        $ch = curl_init();                                  //初始化curl
        curl_setopt($ch, CURLOPT_URL, $postUrl);            //抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);                //设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);                  //post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);                             //运行curl
        curl_close($ch);
        return $data;
    }

    /**
     * 发送get请求
     * @param string $url
     * @return bool|mixed
     */
    function request_get($url = '')
    {
        if (empty($url)) {
            return false;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

 

posted @ 2017-02-14 15:40  unbeau  阅读(2404)  评论(0编辑  收藏  举报