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

php中文网
发布: 2016-07-25 08:57:17
原创
1540人浏览过
本文介绍下,用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 bbs.it-home.org
*/
function getIpInfo($ip,$timeout=15) {
    if(!function_exists('curl_init') or !function_exists('simplexml_load_string')) return false;
    $ch = curl_init("http://bbs.it-home.org/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速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号