php--根据手机号码获取归属地

/**

* 根据手机号码获取归属地

*/

function getMobileLoc($mobile){

    $data = array('province'=>'','city'=>'');    //初始化变量

    //根据百度的数据库调用返回值

    header("Content-type:text/html;charset=utf-8");

    $url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={$mobile}&resource_id=6004&ie=utf8&oe=utf8&format=json";

    $result = curl_https($url);

    $result = (array)json_decode($result);

    if (!empty($result['data'])) {

        $result = (array)$result['data'][0];

        $data['city'] = empty($result['city']) ? '' : $result['city'];

        $data['province'] = empty($result['prov']) ? $result['city'] : $result['prov'];

    }

    return $data;


    /*

    $url = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel={$mobile}&t=".time();

    preg_match_all("/(\w+):'([^']+)/", $content, $matches);

    $data = array_combine($matches[1], $matches[2]);

    print_r($data); 

    exit;

    */       

}


/**

 * curl请求

 */

function curl_https($url){

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

    curl_setopt($ch, CURLOPT_HEADER, FALSE);

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_REFERER, $url);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

    $result = curl_exec($ch);

    curl_close($ch);


    //-------请求为空

    if(empty($result)){

        return false;

    }

    

    return $result;

} 

 

你可能感兴趣的:(PHP)