function getLocation($tel)
{if ( !isPhoneNumber($tel) ) return ['code'=>200,'status'=>false,'msg'=>'Cell phone number error!'];$url = 'http://mobsec-dianhua.baidu.com/dianhua_api/open/location?tel='.$tel;$res = curlRequest($url,'','GET');if ( $res['code'] !== 200 ) return ['code'=>$res['code'],'status'=>false,'msg'=>$res['responseHeader']['msg']];$data = $res['response'][$tel];if ( !$data ) return ['code'=>200,'status'=>false,'msg'=>'API Exception!'];$response['province'] = $data['detail']['province']; $response['city'] = $data['detail']['area'][0]['city']; $response['service'] = $data['detail']['operator']; $response['fullname'] = $data['location']; return ['code'=>200,'status'=>true,'data'=>$response];
}
function isPhoneNumber($tel)//手机号码正则表达试
{return (preg_match("/0?(13|14|15|17|18|19)[0-9]{9}/",$tel))?true:false;
}
function curlRequest($url,$data = '',$method = 'POST')
{$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch,CURLOPT_HTTPHEADER,array("X-HTTP-Method-Override: $method"));curl_setopt($ch, CURLOPT_POSTFIELDS, $data);$document = curl_exec($ch);$code = curl_getinfo($ch,CURLINFO_HTTP_CODE); curl_close($ch);$document = json_decode(removeBOM($document),true);$document['code'] = $code;return $document;
}
function removeBOM($str = '')
{if (substr($str, 0,3) == pack("CCC",0xef,0xbb,0xbf)) {$str = substr($str, 3);}return $str;
}echo "<pre>";
var_dump( getLocation('18888888888') );exit();