search
Homephp教程php手册Return the city name to query local weather based on IP judgment

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

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

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

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

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version