<?php
function get_ip(){
//判断服务器是否允许$_SERVER
if(isset($_SERVER)){
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
$realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}elseif(isset($_SERVER['HTTP_CLIENT_IP'])) {
$realip = $_SERVER['HTTP_CLIENT_IP'];
}else{
$realip = $_SERVER['REMOTE_ADDR'];
}
}else{
//不允许就使用getenv获取
if(getenv("HTTP_X_FORWARDED_FOR")){
$realip = getenv( "HTTP_X_FORWARDED_FOR");
}elseif(getenv("HTTP_CLIENT_IP")) {
$realip = getenv("HTTP_CLIENT_IP");
}else{
$realip = getenv("REMOTE_ADDR");
}
} return $realip;
} function getIp(){
$ip = get_ip();
if($ip=='127.0.0.1'){
$myIp = 'myip';
}
//初始化
$curl = curl_init();
//设置抓取的url
curl_setopt($curl, CURLOPT_URL, 'http://ip.taobao.com/service/getIpInfo.php?ip='.$myIp);
//设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, 0);
//设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//执行命令
curl_multi_getcontent( $curl );
$data = curl_exec($curl);
//关闭URL请求
curl_close($curl); //显示获得的数据
return json_decode($data,true);
} function weather($chengshi){
$url = 'http://wthrcdn.etouch.cn/weather_mini?city='.urlencode($chengshi);
$html = file_get_contents($url);
$jsondata = gzdecode($html);
$data=json_decode($jsondata,true); $arr=array();
$arr['chengshi']=$data['data']['city'];
$dangtian=$data['data']['forecast'][0];
$arr['gaowen']= str_replace("高温 ",null,$dangtian['high']);
$arr['diwen']= str_replace("低温 ",null,$dangtian['low']);
$arr['tianqi']=$dangtian['type'];
return $arr;
} $area = getIp();
if(isset($area['data']['city'])){
$city = $area['data']['city'];
$weather = weather($city);
print_r($weather);
}

php 接口获取公网ip并获取天气接口信息的更多相关文章

  1. 获取公网ip,获取用户城市地址

    <?php class GetIp { public static $api = 'http://ip.taobao.com/service/getIpInfo.php?ip='; public ...

  2. C#联机获取公网IP

    C#获取IP的方式有很多种,这里通过http://www.ipip.net/这个稳定的在线IP库的来获取公网IP. string tempip = "0.0.0.0"; WebRe ...

  3. python获取公网ip,本地ip及所在国家城市等相关信息收藏

    python获取公网ip的几种方式       from urllib2 import urlopen   my_ip = urlopen('http://ip.42.pl/raw').read() ...

  4. Python 之自动获取公网IP

    Python 之自动获取公网IP 2017年9月30日 文档下载:https://wenku.baidu.com/view/ff40aef7f021dd36a32d7375a417866fb84ac0 ...

  5. Delphi获取公网IP地址函数

    uses IdHTTP; function GetPublicIP: string; var strIP, URL: string; iStart, iEnd: Integer; MyIdHTTP: ...

  6. 获取本地ip和获取公网ip

    import socket def get_local_ip(): ''' 获取本地ip地址 :return: ''' s = socket.socket(socket.AF_INET, socket ...

  7. JAVA获取公网ip

    在ipv4地址稀缺的今天,分配到公网ip几乎是不可能的,但是我拨号之后的ip竟然是公网IP. 将自己的电脑作为服务器·,做点好玩的程序,就成为了可能. 由于运营商的ip是动态分配的公网ip的所以就需要 ...

  8. python获取公网ip的几种方式

    python获取公网ip的几种方式 转 https://blog.csdn.net/conquerwave/article/details/77666226 from urllib2 import u ...

  9. 【原创】移动端获取用户公网ip,获取用户ip

    有时候某些api需要获取用户的ip , 特此分享一下获取用户公网ip的方法 纯js <script src="http://pv.sohu.com/cityjson?ie=utf-8& ...

随机推荐

  1. 想要学好Git,应该掌握哪些基础知识?

    说到Git,作为程序员的你,在项目开发中一定会使用到或将来也一定会使用到的,但是我相信,很多在使用Git的人,都只是停留一些简单的操作上,比如提交(commit).拉取(pull).推送(push). ...

  2. SpringBoot整合WEB开发--(六)CROS支持

    简介: CROS(Cross-Origin Resource Sharing)是由W3C制定的一种跨域资源共享技术标准,其目的为了解决前端的跨域请求,在JavaEE开发中,最常见的前端跨域请求解决方案 ...

  3. adb server version (xx) doesn't match this client (xx); killing...

    问题 查看AndroidSDK的adb版本 查看模拟器adb的版本号 安装路径/bin目录下的 nox_adb.exe 将AndroidSDK的adb复制出来,重命名为nox_adb.exe,覆盖模拟 ...

  4. 解决前端js、css缓存问题

    去js标签库查询jquery.i18n.properties.js这个js引用到页面上: 新建一个配置文件:用上面的那个js方法调取配置文件里的版本号给其他的js加上: 示例: <script ...

  5. hashmap与currentHashMap

    hashmap的缺点 多线程不安全,在并发场景下使用时容易出现死循环,脏读问题等 死循环:https://juejin.im/post/5a66a08d5188253dc3321da0 (这篇好点)h ...

  6. Spring MVC3 + Ehcache 缓存实现

    转自:http://www.coin163.com/it/490594393324999265/spring-ehcache Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了 ...

  7. C#中画三角形和填充三角形的简单实现

    C#中画三角形和填充三角形的简单实现: private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graph ...

  8. [AST Babel Plugin] Hanlde ArrowFunction && FunctionExpression

    Continue with previous post: https://www.cnblogs.com/Answer1215/p/12342540.html Now we need to think ...

  9. CentOS 7防火墙快速开放端口配置方法

    CentOS升级到7之后,发现无法使用iptables控制Linuxs的端口,baidu之后发现Centos 7使用firewalld代替了原来的iptables.下面记录如何使用firewalld开 ...

  10. BigInteger和BigDecimal的基本用法

    整型大数 BigInteger: import java.math.BigInteger; import java.util.Scanner; public class Main { public s ...