Home php教程 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

Sep 24, 2016 am 09:02 AM

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)