<?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); }
