|
PHP代码
- <?php
- //获取IP
- function GetIP()
- {
- if(!empty($_SERVER["HTTP_CLIENT_IP"]))
- $cip = $_SERVER["HTTP_CLIENT_IP"];
- else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
- $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
- else if(!empty($_SERVER["REMOTE_ADDR"]))
- $cip = $_SERVER["REMOTE_ADDR"];
- else
- $cip = "无法获取!";
- return $cip;
- }
- /**
- * 通过淘宝IP接口获取IP地理位置
- * @param string $ip
- * @return: string
- **/
- function getCity($ip)
- {
- $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
- $ipinfo=json_decode(file_get_contents($url));
- if($ipinfo->code=='1'){
- return false;
- }
- $city =$ipinfo->data->city;
- //地区 城市
- return $city;
- }
- function foreachFun($d,$type)
- {
- foreach ($d["goods"] as $key => $value) {
- if($value["city_name"] == $type){
- $results = $value["city_code"];
- }
- }
- return $results;
- }
- //function weather_get_time()
- // 从文件中读取数据到PHP变量
- $json_string = file_get_contents('city.json');
- //var_dump($json_string);
- // 用参数true把JSON字符串强制转成PHP数组
- $data = json_decode($json_string, true);
- $IP= GetIP();//获取IP
- $area= getCity($IP); //获取地区
- $cityID=foreachFun($data,$area);
- $html = json_decode(file_get_contents("http://t.weather.sojson.com/api/weather/city/".$cityID));//获取天气json
复制代码 附件在这里:
包含两个文件,一个是PHP文件,一个是地区代码json文件
tianqi.rar
(33.54 KB, 下载次数: 710)
|
|