查询手机归属地phoneAddress.class.php

今天给微信随便加个手机归属地查询功能。

搜了一下。刚好有个API

http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=18850221102

 {
 mts:'1885022',
 province:'福建',
 catName:'中国移动',
 telString:'18850221102',
 areaVid:'30519',
 ispVid:'3236139',
 carrier:'福建移动'
 }
只可惜返回的不是JSON,好吧。我们的思路是采取正则匹配来提取内容代码如下:


<?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()); 
?>


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值