前一阵子看到了一个非常好的php手机归属地查询的类,写的很精简,查询也很精确!大致代码是这样的:
header("Content-type:text/html;charset=utf-8");
$mobile_home=new mobile_home();
$mobile_home->set_mobile('13217248888');
$arr = $mobile_home->showji();
echo "
";
print_r($arr);
class mobile_home{
public $mobile;
public function set_mobile($mobile) {
$this->mobile=$mobile;
$this->checklen();
}
public function checklen(){
if(strlen($this->mobile) != "11"){
}
}
public function showji(){
$data=$this->get('http://api.showji.com/Locating/www.showji.com.aspx?m='.$this->mobile.'&output=json','',"http://api.showji.com");
if(is_array($data)){
return $data;
}
}
private function get($urll, $cookie='',$referer=''){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urll);
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_REFERER ,$referer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
$content = curl_exec($ch);
curl_close($ch);
return json_decode($content,true);
}
}
?>
以下是查询返回的结果,是一个数组,所以很好调用。
Array
(
[Mobile] => 13217244283
[QueryResult] => True
[Province] => 湖北
[City] => 荆门
[AreaCode] => 0724
[PostCode] => 448000
[Corp] => 中国联通
[Card] =>
)