


WeChat public account weather query interface example, public weather query example_PHP tutorial
Jul 12, 2016 am 08:54 AM微信公眾號(hào)天氣查詢接口實(shí)例,公眾天氣查詢實(shí)例
這是來(lái)自百度API的一個(gè)天氣查詢的接口,支持歷史7天和未來(lái)4天的天氣情況查詢,接口不錯(cuò),分享給大家。
接口申請(qǐng)地址:http://apistore.baidu.com/apiworks/servicedetail/112.html
首先,獲取一下apikey(貌似需要手機(jī)驗(yàn)證碼認(rèn)證下)
我調(diào)用的是歷史7天和未來(lái)4天的接口,接口地址是:
http:<span>//</span><span>apis.baidu.com/apistore/weatherservice/recentweathers</span>
我將這個(gè)天氣查詢的功能集成在了微信公眾平臺(tái)上,支持未來(lái)4天天氣查詢,包括:當(dāng)前溫度、pm值、風(fēng)向、風(fēng)力、最高溫度、最低溫度、天氣狀態(tài)的查詢,效果如下:
閑話不多說(shuō),我就喜歡來(lái)點(diǎn)直接的,核心代碼如下:
<span> 1 <span>//<span>接收到文本信息 <span> 2 <span>private <span>function receiveText(<span>$object<span>){ <span> 3 <span>//<span>多個(gè)空格縮為一個(gè) <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"."當(dāng)前溫度:".<span>$citytemp."\n\r"."天氣情況:".<span>$citytype."\n\r"."PM2.5值:".<span>$citypm."\n\r風(fēng)向風(fēng)力:".<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風(fēng)力:".<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 '沒(méi)有空格'; <span>46 if($keyword=="天氣"){ 47 $dateArray = array(); 48 $dateArray[] = array("Title"=>"查詢格式錯(cuò)誤","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

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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

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

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

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

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

The next time you roll your eyes at a weather forecast that went wrong, remember that predicting weather is one of the most complex problems in science. Now, Google has put artificial intelligence into the role of weather forecaster and demonstrated that it can make accurate predictions up to 10 days in advance in just one minute. The task typically takes a roomful of supercomputers several hours to complete. The famous butterfly effect hypothesis states that whether a storm brews can be affected by something as tiny as the flapping of a butterfly's wings on the other side of the world. The job of a weather forecaster is to translate information about these proverbial butterflies into accurate models that tell you whether you should go ahead and plan your picnic for next Saturday. Doing so involves what's called numerical weather prediction (NWP), which uses current weather observations from around the world. as
