Get the App
SLTechnology News&Howtos  ›  Development  › 

How to collect Weather Forecast Information of the whole country by php

Shulou Source: shulou.com Published: 2022-06-03 18:50:50 10月01日 Update

This article introduces the relevant knowledge of "how to collect weather forecast information from the whole country by php". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Weather forecast information collected from the Central Meteorological Station, accurate information, wide coverage, simple code, the return is json code, can be used for client-side calls, can also be displayed after server-side processing, the author's wordpress weather forecast plug-in is the use of this code, it should be noted that this code will generate cache files in the server, need to create a new data folder in the current directory to ensure that the folder can be written.

The php weather forecast code is as follows:

The copy code is as follows:

/ *

Author: freemouse

Home page: www.cnphp.info

API demo: http://www.cnphp.info/tianqi

Usage: you can directly call the above address to display the local weather information, or you can use it this way.

Http://www.cnphp.info/tianqi/?q= Nanjing, Jiangsu Province

* * /

$p = $_ get ['q']

$k = 0

$encoding = mb_detect_encoding ($p)

If ($encoding! = "utf-8") {

$p = mb_convert_encoding ($p, "utf-8", "gbk")

}

$p_arr = array (

"01" > "Beijing"

"02" = > "Shanghai"

"03" > "Tianjin"

"04" > "Chongqing"

"05" = > Heilongjiang

"06" = > "Jilin"

"07" > "Liaoning"

"08" > "Inner Mongolia"

"09" = > "Hebei"

"10" > "Shanxi"

"11" > "Shaanxi"

"12" > "Shandong"

"13" > "Xinjiang"

"14" > "Xizang"

"15" = > Qinghai

"16" = > "Gansu"

"17" > "Ningxia"

"18" > "Henan"

"19" > "Jiangsu"

"20" > "Hubei"

"21" > "Zhejiang"

"22" > "Anhui"

"23" > "Fujian"

"24" > "Jiangxi"

"25" = > Hunan

"26" > "Guizhou"

27 "= >" Sichuan "

"28" > "Guangdong"

"29" > "Yunnan"

"30" > "Guangxi"

"31" > "Hainan"

"32" > "Hong Kong"

"33" > "Macau"

"34" > "Taiwan"

);

Function find (& $item,$key,$data) {

Global $k

If (preg_match ("/ $item/u", $data)) {

$k = $key

}

}

Function get_data_arr ($key) {

If (! file_exists (". / data/city {$key} .xml") {

C = file_get_contents ("http://m.weather.com.cn/data5/city{$key}.xml")

File_put_contents (". / data/city {$key} .xml", $c)

}

Else {

C = file_get_contents (". / data/city {$key} .xml")

}

$arr = explode (",", $c)

Foreach ($arr as $v) {

$data = explode ("|", $v)

$ret [$data [0]] = $data [1]

}

Return $ret

}

Array_walk ($pendant arr.findcards, etc.)

Array_walk (get_data_arr ($k), 'find',$p)

Array_walk (get_data_arr ($k), 'find',$p)

$ccode = get_data_arr ($k)

Echo file_get_contents ("http://m.weather.com.cn/data/{$ccode[$k]}.html");"

This is the end of the content of "how to collect Weather Forecast Information from the whole country by php". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Code Weather Information Weather Forecast Forecast document National content folder more Server knowledge Jiangsu utf-8 Service Learning Achievement Meteorological Observatory Ribbon coverage Black Dragon Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MariaDB Microsoft Shulou Technology vpn