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

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

little bottle
little bottleforward
2019-04-22 17:26:104668browse

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;

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete