今天给微信随便加个手机归属地查询功能。
搜了一下。刚好有个API
http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=18850221102
{ | |
mts:'1885022', | |
province:'福建', | |
catName:'中国移动', | |
telString:'18850221102', | |
areaVid:'30519', | |
ispVid:'3236139', | |
carrier:'福建移动' | |
} |
<?php
/*
phoneAddress手机归属地查询类
Date : 2014/12/9
Author : xeay123
*/
/*
__GetZoneResult_ = { mts:'1885022', province:'福建', catName:'中国移动',
telString:'18850221102', areaVid:'30519',
ispVid:'3236139', carrier:'福建移动' }
思路,分析正则改怎么写。取出所需要的东西
*/
class phoneAddress{ //电话归属地,运营商查询
private $phoneNumber;
private $source;
private $province;
private $catName;
function __construct($phoneNumber){
$this->phoneNumber = $phoneNumber;
$url = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=".$this->phoneNumber;
$this->source = file_get_contents($url);
$this->source = iconv('GBK','utf-8',$this->source);//转为UTF-8
//echo $this->source;
}
function getInfomation(){//获取归属地,运营商
$rule = "/province:'(.*)'/xsU";
preg_match($rule,$this->source,$res);
$this->province = $res[0];
$rule = "/catName:'(.*)'/xsU";
preg_match($rule,$this->source,$res);
$this->catName = $res[0];
}
function getResult(){
self::getInfomation(); //类内方法调用类内方法
return array('province'=>$this->province,'catName'=>$this->catName);
}
}
$obj = new phoneAddress('18850221103');
print_r($obj->getResult());
?>