PHP7中文手册2018 带注释 最新chm版
header("Content-type:text/html;charset=utf-8"); $city = getCity('58.60.130.181'); $city = $city['city']; $url = "http://api.map.baidu.com/telematics/v3/weather?location={$city}&output;=json&ak;=5slgyqGDENN7Sy7pw29IUvrZ"; $file = file_get_contents($url); $array = json_decode($file); foreach($array->results as $key) { echo $key->currentCity; foreach($key->weather_data as $r) { echo $r->date."
"; echo ' '; echo ''.'
'; echo $r->weather."
"; echo $r->wind."
"; echo $r->temperature."
"; echo "
"; } } /** * 获取用户真实 IP */ function getIP() { static $realip; if (isset($_SERVER)){ if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){ $realip = $_SERVER["HTTP_X_FORWARDED_FOR"]; } else if (isset($_SERVER["HTTP_CLIENT_IP"])) { $realip = $_SERVER["HTTP_CLIENT_IP"]; } else { $realip = $_SERVER["REMOTE_ADDR"]; } } else { if (getenv("HTTP_X_FORWARDED_FOR")){ $realip = getenv("HTTP_X_FORWARDED_FOR"); } else if (getenv("HTTP_CLIENT_IP")) { $realip = getenv("HTTP_CLIENT_IP"); } else { $realip = getenv("REMOTE_ADDR"); } } return $realip; } /** * 获取 IP 地理位置 * 淘宝IP接口 * @Return: array */ function getCity($ip) { $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip; $ip=json_decode(file_get_contents($url)); if((string)$ip->code=='1'){ return false; } $data = (array)$ip->data; return $data; }
转载请注明:谷谷点程序 » php利用百度地图api获取天气预报