1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
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();
|