Traditional Culture Encyclopedia - Weather inquiry - How to use PHP to call API interface to realize weather query function
How to use PHP to call API interface to realize weather query function
1, query method:
Baidu provides weather information according to latitude and city name.
2. Interface example:
3. Description of interface parameters:
4. Return the result description:
5、
//City name
$ City? =? Shanghai';
//Encode the string in json format.
$arr? =json_decode($str,TRUE); ?
print _ r($ ATR);
//City name
$city =' Shanghai';
//Get data in json format
$ str = file _ get _ contents("/telematics/v3/weather? Location = ". $ city. "& output = JSON & AMPAK = 5slgyqgdenn7sy7pw29iuvrz");
//Encode the string in json format.
$arr =json_decode($str,TRUE); ?
print _ r($ ATR);
6. The returned page is json encoded data:
[Normal] View normal copy printing?
& ltmeta? charset="UTF-8 " >
rank
(
[error]? = & gt? 0
[Status]? = & gt? success
[Date]? = & gt? 20 14-03- 17
[Result]? = & gt? rank
(
[0]? = & gt? rank
(
[current city]= & gt; ? Shanghai
[Weather data] = > ? rank
(
[0]= >? rank
(
[Date]? = & gt? Monday (today,? Real time: 19℃)
[dayPictureUrl]? = & gt/images/weather/day/qing.png
[nightPictureUrl]? = & gt/images/weather/night/qing.png
[weather]? = & gt? very good
[wind]? = & gt? Southwest wind 3-4
[temperature]? = & gt? 13℃
)
[ 1]? = & gt? rank
(
[Date] = > ? Tuesday
[dayPictureUrl]? = & gt/images/weather/day/duoyun.png
[nightPictureUrl]? = & gt? /images/ weather/night /yin.png
[Weather] = > ? Cloudy to cloudy
[wind] = > ? The northeast wind is 3-4
[temperature]? = & gt? 24? ~? 9℃
)
[2]? = & gt? rank
(
[Date] = > ? Wednesday
[dayPictureUrl]? = & gt/images/weather/day/zhong Yu . png
[nightPictureUrl]? = & gt? /pictures/weather/night/light rain. png
[Weather] = > ? Moderate rain to light rain
[wind] = > ? The northeast wind is 3-4
[temperature]? = & gt? 15? ~? 8℃
)
[3]? = & gt? rank
(
[Date] = > ? Thursday
[dayPictureUrl]? = & gt/images/weather/day/duoyun.png
[nightPictureUrl]? = & gt/images/weather/night/qing.png
[Weather] = > ? cloudy to sunny
[wind] = > ? The north wind is 3-4
[temperature]? = & gt? 14? ~? 6℃
)
)
)
)
)
& ltmeta charset="UTF-8 " >
rank
(
[Error] = > 0
[status] = > success
[Date] = > 20 14-03- 17
[Result] = > rank
(
[0] = > arrangement
(
[current city]= & gt; Shanghai
[Weather data] = > rank
(
[0]= > arrangement
(
? [Date] = > Monday (today, real time: 19℃)
? [dayPictureUrl]= & gt; /images/weather/day/qing.png
? [nightPictureUrl]= & gt; /images/weather/night/qing.png
[Weather] = > very good
? [wind] = > Southwest wind 3-4
? [temperature] = > 13℃
)
[1] = > arrangement
(
[Date] = > Tuesday
? [dayPictureUrl]= & gt; /images/weather/day/duoyun.png
? [nightPictureUrl]= & gt; /images/ weather/night /yin.png
[Weather] = > Cloudy to cloudy
[wind] = > The northeast wind is 3-4
? [Temperature] = >24 ~ 9℃
)
[2] = > arrangement
(
[Date] = > Wednesday
? [dayPictureUrl]= & gt; /images/weather/day/zhong Yu . png
? [nightPictureUrl]= & gt; /pictures/weather/night/light rain. png
[Weather] = > Moderate rain to light rain
[wind] = > The northeast wind is 3-4
? [temperature] = > 15 ~ 8℃
)
[3] = > arrangement
(
[Date] = > Thursday
? [dayPictureUrl]= & gt; /images/weather/day/duoyun.png
[nightPictureUrl]= & gt; /images/weather/night/qing.png
[Weather] = > cloudy to sunny
[wind] = > The north wind is 3-4
? [temperature] = > 14 ~ 6℃
)
)
)
)
)
7.PHP comes with a built-in function that handles json format strings. Let's make an example and give the complete code:
[php] View plain text printing?
& lt Metacharset ="UTF-8 ">
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
//City name
$ City? =? Shanghai';
//Get data in json format
$str? =? file _ get _ contents("/telematics/v3/weather? Location = ". $ city. "& output = JSON & AMPAK = 5slgyqgdenn7sy7pw29iuvrz");
//Encode the string in json format.
$arr? =? json_decode($str,TRUE); ?
Echo? "City:". $ arr ['result'] [0] ['current city']. "? Date: ". $arr['date']。 “& ltbr? /& gt; & ltbr? />;
foreach($ arr[' results '][0][' weather _ data ']as? $val)
{
Echo? $val['date']。 “& ltbr/>;
Echo? "Weather: {$ val ['weather']} < br/>;
Echo? "Wind direction: {$ val ['wind']} < br/>;
Echo? "Temperature: {$ val ['temperature']}
}
& gt
& lt Metacharset ="UTF-8 ">
& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)
//City name
$city =' Shanghai';
//Get data in json format
$ str = file _ get _ contents("/telematics/v3/weather? Location = ". $ city. "& output = JSON & AMPAK = 5slgyqgdenn7sy7pw29iuvrz");
//Encode the string in json format.
$arr = json_decode($str,TRUE); ?
Echo "City:". $ arr ['result'] [0] ['current city']. "Date:". $ arr ['date']。 "
foreach($ arr[' results '][0][' weather _ data ']as $ val)
{
echo $val['date']。 “& ltbr/>;
echo " Weather:{ $ val[' Weather ']} < br/>;
Echo "wind direction: {$ val ['wind']} < br/>;
echo " temperature:{ $ val[' temperature ']} < br/& gt; & ltbr />;
}
& gt
8. The contents returned are as follows:
- Previous article:Recent weather
- Next article:Poems about cold rain
- Related articles
- What are the scenic spots in the summer resort in Chongqing?
- The weather in boxue road, Xinzheng
- Can I travel to Lijiang, Yunnan on May Day? Will the 20 19 Lijiang mountain fire affect Old Town of Lijiang?
- What are the attractions worth visiting on the 11th National Day?
- Gaga. Why is Xu Tianqi called Gaga?
- Opening hours of swimming pool in Lixian Gymnasium
- Is UFO real or fake?
- When is the best time to go to Yulong Snow Mountain in Yunnan?
- Are wooden cows and flowing horses real?
- Weather conditions in Dongsheng, Inner Mongolia