Home > Backend Development > PHP Tutorial > PHP implements method to obtain weather in specified area

PHP implements method to obtain weather in specified area

墨辰丷
Release: 2023-03-28 06:22:01
Original
1499 people have browsed it

This article mainly introduces the relevant information of PHP to obtain the weather instance code of a specified area. Friends who need it can refer to

PHP to obtain the weather of a specified area

Weather query is used when developing the website. Since it is based on WordPress, there are many restrictions. First create a [weather.PHP] file, and then look at the code:

<?php  
//获取天气 
   $url = &#39;http://m.weather.com.cn/data/&#39;;  
   $id = &#39;101181101&#39;; //焦作的代号 
   $data = file_get_contents($url . $id .&#39;.html&#39;);  
 
 $obj=json_decode($data); 
 echo $obj->weatherinfo->city.&#39;:&#39;.$obj->weatherinfo->weather1.&#39; &#39;.$obj->weatherinfo->temp1;
Copy after login

For:

$url = &#39;http://m.weather.com.cn/data/&#39;;  
   $id = &#39;101181101&#39;; //焦作的代号 
   $data = file_get_contents($url . $id .&#39;.html&#39;);
Copy after login

can be abbreviated as:

$data = file_get_contents(&#39;http://m.weather.com.cn/data/101181101.html&#39;);
Copy after login

And for:

$obj=json_decode($data);
Copy after login

It converts the obtained json data into an object for easy calling;

Then the last sentence:

echo $obj->weatherinfo->city.&#39;:&#39;.$obj->weatherinfo->weather1.&#39; &#39;.$obj->weatherinfo->temp1;
Copy after login

is to obtain the specified data and output it in a certain format,

$obj->weatherinfo->city //城市 
$obj->weatherinfo->weather1 //今天的天气 
$obj->weatherinfo->temp1 //今天的气温
Copy after login

Finally put

<?php include &#39;weather.php&#39; ?>
Copy after login

where it needs to be displayed.

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHP uses cURL to call WebServiceGet weatherinformation

PHP APIGet weatherForecast, and use Fetion API to send friends _PHP tutorial

php ajax to achieve no refreshGet WeatherStatus_PHP Tutorial

The above is the detailed content of PHP implements method to obtain weather in specified area. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template