教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php curl获取指定IP所有信息的API代码

php curl获取指定IP所有信息的API代码

发布时间:2016-09-13   编辑:jiaochengji.com
本文介绍下,用php中的curl函数取得指定IP地址下所有信息的API代码,有需要的朋友参考下。

分享下用curl获取指定IP下所有信息的api代码。

有关curl的相关内容,可以参考以下文章:
php curl上传文件的简单例子
php curl post的简单示例
php curl应用实例分析
php curl用法的实例代码
php使用curl判断远程文件是否存在的代码
php使用curl伪造IP来源的代码
php curl 学习总结

先充充电,然后看下面的代码。

代码如下:

<?php
/**
* curl取指定IP信息的API代码
* edit by www.jbxue.com
*/
function getIpInfo($ip,$timeout=15) {
    if(!function_exists('curl_init') or !function_exists('simplexml_load_string')) return false;
    $ch = curl_init("http://www.jbxue.com/ip_query2.php?ip={$ip}&timezone=true");
    $options = array(
        CURLOPT_RETURNTRANSFER => true,
    );
    curl_setopt_array($ch,$options);
    $res = curl_exec($ch);
    curl_close($ch);
    if($xml = simplexml_load_string($res)) {
        $dt = array();
        foreach ($xml->Location->children() as $key=>$item)  {
            $dt[$key] = strtolower($item);
        }
        return $dt;
    } else {
        return false;
    }
}
$current_Ip_Info = getIpInfo('61.164.140.55');
var_dump($current_Ip_Info);  

/*
返回内容:
array(13) {
  ["Ip"]=>
  string(13) "61.164.140.55"
  ["Status"]=>
  string(2) "ok"
  ["CountryCode"]=>
  string(2) "cn"
  ["CountryName"]=>
  string(5) "china"
  ["RegionCode"]=>
  string(2) "02"
  ["RegionName"]=>
  string(8) "zhejiang"
  ["City"]=>
  string(5) "ruian"
  ["ZipPostalCode"]=>
  string(0) ""
  ["Latitude"]=>
  string(7) "27.7814"
  ["Longitude"]=>
  string(7) "120.628"
  ["TimezoneName"]=>
  string(14) "asia/chongqing"
  ["Gmtoffset"]=>
  string(5) "28800"
  ["Isdst"]=>
  string(1) "0"
}
*/
?>

您可能感兴趣的文章:
php curl获取指定IP所有信息的API代码
php 获取远程网页内容简单函数
php根据ip调用新浪api获取城市名转拼音
怎么用PHP定位IP所在地
php函数获取在线ip与客户端ip
php伪造ip与防止伪造ip方法解析
php file_get_contents函数抓取页面信息的代码
PHP利用Curl模拟登录并获取数据例子
php通过IP获取地理位置
PHP fopen/file_get_contents与curl性能比较

[关闭]
~ ~