微信公众号天气查询接口实例,公众天气查询实例
微信公众号天气查询接口实例,公众天气查询实例
这是来自百度API的一个天气查询的接口,支持历史7天和未来4天的天气情况查询,接口不错,分享给大家。
接口申请地址:http://apistore.baidu.com/apiworks/servicedetail/112.html
首先,获取一下apikey(貌似需要手机验证码认证下)
我调用的是历史7天和未来4天的接口,接口地址是:
http:<span>//</span><span>apis.baidu.com/apistore/weatherservice/recentweathers</span>
我将这个天气查询的功能集成在了微信公众平台上,支持未来4天天气查询,包括:当前温度、pm值、风向、风力、最高温度、最低温度、天气状态的查询,效果如下:
闲话不多说,我就喜欢来点直接的,核心代码如下:
<span> 1 <span>//<span>接收到文本信息 <span> 2 <span>private <span>function receiveText(<span>$object<span>){ <span> 3 <span>//<span>多个空格缩为一个 <span> 4 <span>$keyword = <span>$object-><span>Content; <span> 5 <span>$keyword=<span>preg_replace("/\s(?=\s)/","\\1",<span>$keyword<span>); <span> 6 <span>$funcFlag = 0<span>; <span> 7 <span>if(<span>strpos(<span>$keyword," "<span>)){ <span> 8 <span>//<span>echo '有空格'; <span> 9 <span>$arr = <span>explode(" ",<span>$keyword<span>); <span>10 <span>if(<span>$arr[0]=="天气"<span>){ <span>11 <span>$dateArray = <span>array<span>(); <span>12 <span>$ch =<span> curl_init(); <span>13 <span>$city=<span>urlencode(<span>$arr[1<span>]); <span>14 <span>$url = 'http://apis.baidu.com/apistore/weatherservice/recentweathers?cityname='.<span>$city<span>; <span>15 <span>$header = <span>array<span>( <span>16 'apikey: 6512ad3a465e3a9e3d0d1da56639626c', <span>17 <span> ); <span>18 curl_setopt(<span>$ch, CURLOPT_HTTPHEADER , <span>$header<span>); <span>19 curl_setopt(<span>$ch, CURLOPT_RETURNTRANSFER, 1<span>); <span>20 curl_setopt(<span>$ch , CURLOPT_URL , <span>$url<span>); <span>21 <span>$res = curl_exec(<span>$ch<span>); <span>22 <span>$data = json_decode(<span>$res,<span>true<span>); <span>23 <span>$cityname=<span>$data['retData']['city'<span>]; <span>24 <span>$citydate=<span>$data['retData']['today']['date'<span>]; <span>25 <span>$cityweek=<span>$data['retData']['today']['week'<span>]; <span>26 <span>$citytemp=<span>$data['retData']['today']['curTemp'<span>]; <span>27 <span>$citypm=<span>$data['retData']['today']['aqi'<span>]; <span>28 <span>$cityhightemp=<span>$data['retData']['today']['hightemp'<span>]; <span>29 <span>$citylowtemp=<span>$data['retData']['today']['lowtemp'<span>]; <span>30 <span>$citytype=<span>$data['retData']['today']['type'<span>]; <span>31 <span>$cityfengli=<span>$data['retData']['today']['fengli'<span>]; <span>32 <span>$cityfengxiang=<span>$data['retData']['today']['fengxiang'<span>]; <span>33 <span>$cityforecast=<span>$data['retData']['forecast'<span>]; <span>34 <span>35 <span>$out="今日温度:".<span>$citylowtemp."~".<span>$cityhightemp."\n\r"."当前温度:".<span>$citytemp."\n\r"."天气情况:".<span>$citytype."\n\r"."PM2.5值:".<span>$citypm."\n\r风向风力:".<span>$cityfengxiang." ".<span>$cityfengli<span>; <span>36 <span>$dateArray[] = <span>array("Title"=><span>$cityname."\n\r".<span>$citydate." ".<span>$cityweek,"Description"=>"","Picurl"=>"","Url" =>""<span>); <span>37 <span>$dateArray[] = <span>array("Title"=><span>$out,"Description"=>"","Picurl"=>"","Url" =>""<span>); <span>38 <span>for(<span>$i=0;<span>$i<<span>count(<span>$cityforecast);<span>$i++<span>){ <span>39 <span>$outstr="☀ ".<span>$cityforecast[<span>$i]["date"]." ".<span>$cityforecast[<span>$i]["week"]."\n\r气温:".<span>$cityforecast[<span>$i]["lowtemp"]."~".<span>$cityforecast[<span>$i]["hightemp"]."\n\r风力:".<span>$cityforecast[<span>$i]["fengxiang"]." ".<span>$cityforecast[<span>$i]["fengli"]."\n\r天气:".<span>$cityforecast[<span>$i]["type"<span>]; <span>40 <span>$dateArray[] = <span>array("Title"=><span>$outstr,"Description"=>"","Picurl"=>"","Url" =>""<span>); <span>41 <span> } <span>42 <span>$resultStr = <span>$this->transmitNews(<span>$object, <span>$dateArray, <span>$funcFlag<span>); <span>43 <span> } <span>44 }<span>else<span>{ <span>45 <span>//<span>echo '没有空格'; <span>46 if($keyword=="天气"){ 47 $dateArray = array(); 48 $dateArray[] = array("Title"=>"查询格式错误","Description"=>"","Picurl"=>"","Url" =>""); 49 $dateArray[] = array("Title"=>"正确格式例:天气 北京","Description"=>"","Picurl"=>"","Url" =>""); 50 $resultStr = $this->transmitNews($object, $dateArray, $funcFlag); 51 } 52 } 53 return $resultStr; 54 }</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
查询格式:“天气 城市名”

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



As a popular back-end programming language, PHP is widely popular in the field of web development. The weather forecast function is a common web application scenario. Implementing the weather forecast function based on PHP is relatively simple and easy to understand. This article will introduce how to use PHP to implement the weather forecast function. 1. Obtain weather data API To implement the weather forecast function, you first need to obtain weather data. We can use third-party weather APIs to obtain real-time, accurate weather data. At present, the mainstream weather API providers in China include the free “Xinzhiwei” and

Since December 2021, Huawei & Honor mobile phones have launched the Vientiane desktop widget function. Many convenient functions, visually optimized desktop controls, etc. have been added to many users’ mobile desktops; by August this year, the two major merchant platforms also opened up sports and health data, weather data, music data, system data, etc., allowing users to use their mobile desktops The interactive operation is more convenient, faster and more interesting, allowing users to DIY and create their own personalized desktop. Mobile desktop after adding widgets Recently, many Huawei mobile phone users have reported that they are not clear about how to add desktop widgets on Huawei and Honor mobile phones, complaining that the process is too complicated and cumbersome. In order to help everyone solve this problem, Qian Shuxian has prepared a detailed operation process, hoping to

How to use JavaWebSocket to implement real-time weather forecast function? With the popularity of the Internet and mobile devices, real-time weather forecast function has become one of the essential functions of many applications. Using JavaWebSocket technology can realize real-time communication conveniently and quickly, providing users with the latest weather forecast information. This article will introduce how to use JavaWebSocket to implement the real-time weather forecast function and provide specific code examples. Environment preparation Before starting, you need to make sure that you have installed

IT House reported on November 3 that Google Research and DeepMind collaborated to develop the latest weather model MetNet-3. This model is based on the previous MetNet and MetNet-2. It can predict global weather conditions 24 hours in advance. High-resolution forecasts including precipitation, surface temperature, wind speed, wind direction and felt temperature. IT House found that Google mentioned that the MetNet-3 model has been implemented in the "Google Mobile Software" weather forecast on the mobile platform. The MetNet-3 model can create "smooth and high-precision" predictions with a spatial resolution of 1 to 4 kilometers and an analysis interval of 2 minutes. Experiments have proven that MetNet-3's prediction capabilities surpass traditional physical weather forecast models. For example

According to news on November 3, Google Research and DeepMind collaborated to develop the latest weather model MetNet-3. This model is based on the previous MetNet and MetNet-2 and can make high-resolution predictions of global weather conditions 24 hours in advance. , including precipitation, surface temperature, wind speed, wind direction and felt temperature. This site found that Google mentioned that the MetNet-3 model has been implemented in the "Google Mobile Software" weather forecast on the mobile platform. The MetNet-3 model can create "smooth and highly accurate" forecasts with a spatial resolution of 1 to 4 kilometers and a 2-minute analysis interval. Experiments have proven that the prediction ability of MetNet-3 surpasses traditional physical weather forecast models, such as

There will be a weather forecast function on the desktop, so how to restore the desktop weather forecast? Users need to click Window Tools on the desktop, then click Weather, and select a weather component. This desktop weather forecast recovery method introduction will tell you the specific operation method. The following is a detailed introduction, take a look! How to restore desktop weather forecast? Answer: Select weather in the desktop widget, and then add it to the desktop. Specific method: ios method: 1. Long press the Apple screen, and then click the + sign in the upper left. 2. Search for weather and open the app. 3. Then select the style of the weather component and click Add Widget. 4. Then click Done on the upper right. Android method: 1. Long press the screen and click on the widget. 2. Click on the sky

How to obtain city weather forecast information using Python and Baidu Map API? Introduction: Weather forecast plays an important guiding role in our daily life. This article will introduce how to use the Python programming language and Baidu Map API to obtain weather forecast information for a city, thereby helping us understand the weather conditions accurately. First, we need to register a Baidu developer account and create an application. After creating the application, Baidu will provide us with a pair of APIKeys for permission authentication using the Baidu Map API. catch

Editor | In the era of ScienceAI large models, the effects of purely data-driven meteorological and climate models are gradually catching up to or even surpassing numerical models. However, existing large-scale meteorological and climate models still have some problems. For example, the physical consistency in the model is not high enough, which limits the ability to predict complex weather and climate phenomena such as precipitation. In addition, the forecast effect of divergent wind is not satisfactory. These issues require further research and improvement to improve the prediction accuracy and reliability of the model. At present, combining physics, atmospheric dynamics and deep learning models is an important way to solve the bottleneck problem. Recently, the team of researcher Huang Gang from the Institute of Atmospheric Physics, Chinese Academy of Sciences, based on the data and computing power support of the Earth System Numerical Simulation Facility (Huan), analyzed the coupling relationship between physical variables from the perspective of
