PHP获取手机号归属地,PHP获取2017最新手机号段属地类

这是一个PHP类,用于根据手机号码获取归属地信息。通过读取手机号码段文本文件并生成缓存,提高查询效率。类中包含了读取文件、创建缓存文件以及获取手机归属地的方法。
摘要由CSDN通过智能技术生成


<?php
/**
php获取手机号段归属地类 2017
@author  sms.cn
@link  http://www.sms.cn
@date  2017/4/9
*/
class MobileCity {

 /**
 * 缓存文件保存目录
 */
 public $CACHE_DIR;

 /**
 * 手机号段归属地文本文件
 */
 public $MOBILE_FILE;
 /**
 * 文本文件每行间隔符
 */
 public $ROW_SPACE;
 /**
 * 文本文件列数据字段间隔符
 */
 public $LINE_SPACE;


 /**
 * 文本文件数据格式对应表字段
 *
 * @var string
 */
 public $FORMAT;

    public function __construct()
    {
  $this->CACHE_DIR = './mobile_city';
  $this->MOBILE_FILE = './Mobile.txt';
  $this->ROW_SPACE = "\r\n";
  $this->LINE_SPACE = ",";

  //1,"1300000","山东","济南","中国联通","0531","250000"
  //按间隔符切开从0开始对应字段
  //可以根据需要生成字段,不需要注释
  $this->FORMAT = array(
   0=>'',
   1=>'city_mobile',
   2=>'city_province',
   3=>'city_city',
   //4=>'city_com',
   //5=>'city_code',
   //6=>'city_zip',
   );
    }
 /**
 * 文本文件生成缓存文件
 * @return array
 */
    public function writeCache()
    {
  if( !is_file($this->MOBILE_FILE) )
  {
   //return '文件不存在!';
   die('文件不存在!');
  }
  $data = $this->getFile($this->MOBILE_FILE);
  
  $rows = explode($this->ROW_SPACE,$data);
  unset($data);
  foreach( $rows as $j=>$row )
  {
   $row = str_replace(array('"',"'"),'',$row);
   $line = explode($this->LINE_SPACE,$row);
   unset($rows[$j]);
   unset($line_data);
   foreach( $line as $k=>$d )
   {
    if( $d != '' && $this->FORMAT[$k] )
    {
     $line_data[$this->FORMAT[$k]] = $d;
    }
   }
   $mobile = $line_data[city_mobile];
   $pre = substr($mobile,0,3);
   unset($line_data[city_mobile]);
   $data_array[$pre][$mobile]=$line_data;

  }
  unset($rows);
  foreach( $data_array as $pre=>$row )
  {
   $this->writeCacheFile($this->CACHE_DIR,$pre,$row);
   unset($data_array[$pre]);
  }
    }
 /**
 * 创建多级目录
 *
 * @param string $dir 目录
 * @param string $mode 目录权限
 * @return true
 */
 protected function mkdirs($dir,$mode=0777){
  if(!is_dir($dir)){
   $this->mkdirs(dirname($dir), $mode);
   return mkdir($dir, $mode);
  }
  return true;
 }
 /**
 * 读文件内容
 *
 * @param string $file 文件
 * @return true
 */
 protected function getFile($file){
  $f = fopen($file,"r");
  $d = fread($f,filesize($file));
  fclose($f);
  return $d;
 }
 /**
 * 写文件内容
 *
 * @param string $file 文件
 * @param string $data 数据
 * @param string $m 访问类型
 * @return true
 */
 protected function putFile($file,$data,$m='w'){
  $this->mkdirs(dirname($file));
  $f = fopen($file,$m);
  $d = fwrite($f,$data);
  fclose($f);
 }
 /**
 * 数组转数组字符串
 *
 * @param string $obj 数组
 * @param string $mod
 * @return array
 */
 protected function ArrayToString($obj,$mod=1){
  if(empty($obj)) return "array()";
  $objType=gettype($obj);
  if ($objType=='array') {
   $objstring = "array(";
   foreach ($obj as $objkey=>$objv) {
    if($mod==1)  $objstring .= is_numeric($objkey) && strlen($objkey)<8 && $objkey{0}!='0' ? $objkey.'=>':"'".$objkey."'=>";
    $vtype =gettype($objv);
    if ($vtype=='integer') {
     $objstring .="$objv,";
    }else if ($vtype=='double'){
      $objstring .="$objv,";
    }else if ($vtype=='string'){
      $objstring .="'".$objv."',";
    }else if ($vtype=='array'){
      $objstring .=$this->ArrayToString($objv).",\n";
    }else if ($vtype=='object'){
      $objstring .=$this->ArrayToString($objv).",\n";
    }else{
      $objstring .="'".$objv."',";
    }
   }
   return substr($objstring,0,-1).")";
  }
 }

 /**
 * 写PHP数组缓存文件
 *
 * @param string $dir 目录
 * @param string $file 文件
 * @param string $data 数据
 */
 protected function writeCacheFile($dir,$file,$data){
  $data = $this->ArrayToString($data);
  $data = "<?php\n\$mobile_city_cae = ".$data.";\n?>";
  $this->putFile($dir."/".$file.'.cae.php',$data);
 }
 /**
 * 获取手机归属地
 *
 * @param string $m 手机号
 * @return array
 */
 function getMobileCity($m)
 {
  $pre = substr($m,0,3);
  $pre_mobile = substr($m,0,7);
  $file = $this->CACHE_DIR.'/'.$pre.'.cae.php';
  if( is_file($file) )
  {
   include $file;
  }
  if( $mobile_city_cae[$pre_mobile] )
  {
   return $mobile_city_cae[$pre_mobile];
  }
  return array('city_province'=>'');
 }
}

/**/
//$ROOT = dirname(__FILE__);
//$Mcity = new MobileCity();
//缓存文件目录
//$Mcity->CACHE_DIR = $ROOT.'/mobile_city';
//第一次生成缓存文件
//$Mcity->MOBILE_FILE = $ROOT.'/Mobile.txt';
//$Mcity->writeCache();
//print_r($Mcity->getMobileCity('15900001111'));
?>

PHP获取手机号归属地源代码和号段数据下载

http://download.csdn.net/detail/haohao2345/9828511

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
由于获取用户IP地址的方式可能因系统和网络环境而异,因此不能提供简单的Java代码来实现此功能。 其中一种方法是使用第三方API,例如ip-api.com。您可以使用Java的HttpClient发送GET请求到该网站,然后解析响应内容获取用户的IP地址信息。 代码示例: ``` import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import org.json.JSONObject; public class Example { public static void main(String[] args) throws IOException { String url = "http://ip-api.com/json"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); JSONObject json = new JSONObject(response.toString()); System.out.println("Your IP: " + json.getString("query")); System.out.println("Your Location: " + json.getString("city") + ", " + json.getString("regionName") + ", " + json.getString("country")); } } ``` 在这个示例中,我们使用了第三方API ip-api.com 获取ip地址信息,使用了Java核心库中的URL、HttpURLConnection、BufferedReader等发送请求,使用json-simple解析响应内容。 请注意,需要添加json-simple包,详细请参考json-simple官网
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值