亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP基于新浪IP庫獲取IP詳細(xì)地址的方法

 更新時(shí)間:2017年05月04日 08:52:11   作者:yhdsir  
這篇文章主要介紹了PHP基于新浪IP庫獲取IP詳細(xì)地址的方法,涉及php正則、curl及編碼轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP基于新浪IP庫獲取IP詳細(xì)地址的方法。分享給大家供大家參考,具體如下:

<?php
class Tool{
  /**
   * 獲取IP的歸屬地( 新浪IP庫 )
   *
   * @param $ip String    IP地址:112.65.102.16
   * @return Array
   */
  static public function getIpCity($ip)
  {
    $ip = preg_replace("/\s/","",preg_replace("/\r\n/","",$ip));
    $link = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=".$ip."&t=".time();
    $ipJson = self::httpCurl($link);
    preg_match("/\"country\":\"(.*)\"/Uis",$ipJson, $match1);
    preg_match("/\"province\":\"(.*)\"/Uis",$ipJson, $match2);
    preg_match("/\"city\":\"(.*)\"/Uis",$ipJson, $match3);
    return array(
      'country'=>self::ucode2zh($match1[1]), // 國家
      'province'=>self::ucode2zh($match2[1]), // 省
      'city'=>self::ucode2zh($match3[1])   // 城市
    );
  }
  /**
   * Curl方式獲取信息
   */
  static public function httpCurl($url)
  {
    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, $url);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($curl_handle, CURLOPT_FAILONERROR,1);
    $file_content = curl_exec($curl_handle);
    curl_close($curl_handle);
    return $file_content;
  }
  /**
   * 將unicode編碼轉(zhuǎn)化為中文,轉(zhuǎn)化失敗返回原字符串
   *
   * @param $code String   unicode編碼
   * @return String
   */
  static public function ucode2zh($code)
  {
    $temp = explode('\u',$code);
    $rslt = array();
    array_shift($temp);
    foreach($temp as $k => $v)
    {
      $v = hexdec($v);
      $rslt[] = '&#' . $v . ';';
    }
    $r = implode('',$rslt);
    return empty($r) ? $code : $r;
  }
}

獲取IP地址類使用實(shí)例

<?php
$ipStr = Tool::getIpCity('112.65.102.16');
print_r($ipStr);

返回結(jié)果

Array ( [country] => 中國 [province] => 上海 [city] => 上海 )

PS:這里再為大家提供幾款I(lǐng)P地址相關(guān)在線工具供大家參考使用:

IP地址歸屬地在線查詢工具:
http://tools.jb51.net/aideddesign/ipcha

在線網(wǎng)絡(luò)計(jì)算器|TCP/IP子網(wǎng)掩碼計(jì)算與換算工具:
http://tools.jb51.net/aideddesign/ipcalc

在線IP地址/子網(wǎng)掩碼計(jì)算與轉(zhuǎn)換工具:
http://tools.jb51.net/aideddesign/ip_net_calc

在線子網(wǎng)掩碼換算與網(wǎng)絡(luò)計(jì)算工具:
http://tools.jb51.net/aideddesign/network_calc

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php文件操作總結(jié)》、《PHP基本語法入門教程》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論