Home >Backend Development >PHP Tutorial >PHP development example code to obtain the weather in a specified area

PHP development example code to obtain the weather in a specified area

PHPz
PHPzOriginal
2017-03-05 14:23:502062browse

This article mainly introduces the relevant information of PHP to obtain the weather example code of a specified area. Friends in need 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;

For:

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

can be abbreviated as:

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

And for:

$obj=json_decode($data);

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

Then the last sentence:

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

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

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

Finally where it needs to be displayed

<?php include &#39;weather.php&#39; ?>

That’s it.

Thank you for reading the PHP development example code to obtain the weather in a specified area. I hope it can help everyone. Thank you for your support of the PHP Chinese website!

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