Home  >  Article  >  php教程  >  Return the city name to query local weather based on IP judgment

Return the city name to query local weather based on IP judgment

WBOY
WBOYOriginal
2016-09-24 09:02:501582browse

header("content-type:text/html;charset=utf-8");
date_default_timezone_set("Asia/Shanghai");
error_reporting(0);
// Determine the city based on IP
$ user_ip = $_SERVER['REMOTE_ADDR'];
$url ="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$user_ip";
$address = file_get_contents($ url);
$address_arr = json_decode($address); //Return object, needs to be converted to array

//Take Shanghai as an example

function object_array($array){
 if(is_object($array)){
  $array = (array)$array;
 }
 if(is_array($array)){
  foreach($array as $key=> ;$value){
   $array[$key] = object_array($value);
  }
 }
  return $array;
}

//Convert to an array through this function
$address_arr = object_array($address_arr);

// print_r($address_arr);

stdClassObject
(
[ret] => 1

[start] => -1
[end] => -1
[country] => China
[province] => Shanghai
[city] => Shanghai
[district] =>
[isp] =>
[type] =>
[desc] =>
)
Array
(
[ret] => 1
[start] => -1
[end] => -1
[country] => China
[province] => Shanghai
[city] => Shanghai
[district] =>
[isp] =>
[type] =>
[desc] =>
)


$city = $address_arr["city"];

//The output is Shanghai, and the obtained city can query the local weather through Baidu Weather API

$con=file_get_contents("http://api.map.baidu.com/telematics/v3/weather?location=$city&output=json&ak=spmMww7Eoqcmf3FXbnLyDUwL"); //Note that the ak value needs to be registered in the Baidu interface, and the address must be attached :http://lbsyun.baidu.com/apiconsole/key
$con = json_decode($con);

print_r($con);

stdClassObject
(
[error] => 0
[status] => success
[date] => 2016-09-23
[results] => Array
(
[0] => stdClass Object
(
[currentCity] => Shanghai
[pm25] => 42
[index] => Array
(
[0] => stdClass Object
(
[title] => Dressing
[zs] => Hot
[tipt] => Clothing index
[des] => The weather is hot, so it is recommended to wear short skirts, shorts, short thin jackets, T-shirts and other summer clothing.
)
[1] => stdClass Object
(
[title] => Car wash
[zs] => more suitable
[tipt] => Car wash index
[des] => It is more suitable for car washing. There will be no rain in the coming day and the wind will be light. A clean car can last at least one day.
)
[2] => stdClass Object
(
[title] => Travel
[zs] => suitable
[tipt] => Tourism Index
[des] => The weather is good, but it will not affect your mood for traveling at all. The temperature is suitable and there is a breeze, making it suitable for traveling.
)
[3] => stdClass Object
(
[title] => Cold
[zs] => Send less
[tipt] => Cold index
[des] => The weather conditions are suitable, there is no obvious cooling process, and the chance of catching a cold is low.
)
[4] => stdClass Object
(
[title] => Sports
[zs] => more suitable
[tipt] => Sports index
[des] => The weather is fine, please pay attention to sun protection when doing outdoor sports. Indoor exercise is recommended.
)
[5] ​​=> stdClass Object
(
[title] => UV intensity
[zs] => weak
[tipt] => UV intensity index
[des] => The intensity of ultraviolet rays is weak. It is recommended to apply sunscreen skin care products with SPF between 12-15 and PA+ before going out.
)
)
[weather_data] => Array
(
[0] => stdClass Object
(
[date] => Friday, September 23 (real time: 26℃)
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/duoyun.png
[weather] => Cloudy
[wind] => east breeze
[temperature] => 27 ~ 21℃
)
[1] => stdClass Object
(
[date] => Saturday
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/duoyun.png
[weather] => Cloudy
[wind] => east breeze
[temperature] => 28 ~ 23℃
)
[2] => stdClass Object
(
[date] => Sunday
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/yin.png
[weather] => Cloudy to overcast
[wind] => east breeze
[temperature] => 28 ~ 23℃
)
[3] => stdClass Object
(
[date] => Monday
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/yin.png
[weather] => Cloudy to overcast
[wind] => Northeasterly breeze
[temperature] => 29 ~ 25℃
)
   
  )
   
  )
   
  )
   
  )

$arr = object_array($con); //Continue converting to array operation

$detail = $arr["results"][0]["weather_data"];
$city = $arr["results"][0]["currentCity"];

print_r($detail);

Array
(
[0] => Array
(
[date] => Friday, September 23 (real time: 26℃)
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/duoyun.png
[weather] => Cloudy
[wind] => east breeze
[temperature] => 27 ~ 21℃
)
[1] => Array
(
[date] => Saturday
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/duoyun.png
[weather] => Cloudy
[wind] => east breeze
[temperature] => 28 ~ 23℃
)
[2] => Array
(
[date] => Sunday
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/yin.png
[weather] => Cloudy to overcast
[wind] => east breeze
[temperature] => 28 ~ 23℃
)
[3] => Array
(
[date] => Monday
[dayPictureUrl] => http://api.map.baidu.com/images/weather/day/duoyun.png
[nightPictureUrl] => http://api.map.baidu.com/images/weather/night/yin.png
[weather] => Cloudy to overcast
[wind] => Northeasterly breeze
[temperature] => 29 ~ 25℃
)
)

//Get weather data and adjust it according to your needs

?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn