Home > Backend Development > PHP Tutorial > PHP calls Baidu weather interface API to query real-time weather

PHP calls Baidu weather interface API to query real-time weather

little bottle
Release: 2023-04-06 08:54:01
forward
4704 people have browsed it

Now, you don’t have to wait at 7:30 pm to check the weather forecast for the next day. As long as you have a mobile phone and an Internet connection, you can easily check the real-time weather. But do you know how to use PHP? This article will teach you how to call Baidu weather interface to view real-time weather directly on PHP. Without further ado, let’s learn together!

The code is as follows:

//百度天气接口API
$location = "北京";  //地区
$ak = "5slgyqGDENN7Sy7pw29IUvrZ"; //秘钥,需要申请,百度为了防止频繁请求
$weatherURL = "http://api.map.baidu.com/telematics/v3/weather?location=$location&output=json&ak=$ak";   
$ch = curl_init($weatherURL) ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
$result = curl_exec($ch);
echo $result;
Copy after login

Related tutorials: PHP video tutorial

The above is the detailed content of PHP calls Baidu weather interface API to query real-time weather. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template