<?php
Header ("content-type:text/html; Charset=utf-8 ");
function to get IP
function GetIP () {
if (!empty ($_server["Http_client_ip")) {
$cip = $_server["Http_client_ip"];
}
ElseIf (!empty ($_server["Http_x_forwarded_for")) {
$cip = $_server["Http_x_forwarded_for"];
}
ElseIf (!empty ($_server["REMOTE_ADDR")) {
$cip = $_server["REMOTE_ADDR"];
}
else{
$CIP = "Cannot get!" ";
}
return $CIP;
}
$ip = GetIP ();
$urllocation = "http://api.map.baidu.com/location/ip?ak=TFyi03cqGpZAREZbhsboBTzE&ip=". $ip;
$location =json_decode (Send_post ($urllocation), true);
Echo ($location ["Address"]);
Baidu Ipapi Http://developer.baidu.com/map/index.php?title=webapi
Send a POST request
$url = ' Http://apis.baidu.com/heweather/weather/free?city=wenzhou ';
function Send_post ($url) {
$ch = Curl_init ();
$header = Array (
' apikey:7728db103ad7e801f1225d488ddce370 ',
);
Add Apikey to Header
curl_setopt ($ch, Curlopt_httpheader, $header);
curl_setopt ($ch, Curlopt_returntransfer, 1);
Executing an HTTP request
curl_setopt ($ch, Curlopt_url, $url);
return curl_exec ($ch);
}
Weather data exists in the $weather variable
$weahter =json_decode (Send_post ($url))->{"Heweather Data Service 3.0"}[0];
Echo ($weahter->{"Basic"}->{"City"}. " <br> ");
Echo ($weahter->{"Now"}->{"cond"}->{"code"}. " <br> ");
Echo ($weahter->{"Now"}->{"cond"}->{"TXT"}. " <br> ");
Echo ($weahter->{"suggestion"}->{"COMF"}->{"BRF"}. " <br> ");
Echo ($weahter->{"suggestion"}->{"COMF"}->{"TXT"}. " <br> ");
?>
PHP Local Weather