Home Backend Development PHP Tutorial WeChat public account weather query interface example WeChat public account certification WeChat public account template WeChat public account name

WeChat public account weather query interface example WeChat public account certification WeChat public account template WeChat public account name

Jul 29, 2016 am 08:53 AM
WeChat public account

这是来自百度API的一个天气查询的接口,支持历史7天和未来4天的天气情况查询,接口不错,分享给大家。

接口申请地址:http://apistore.baidu.com/apiworks/servicedetail/112.html

首先,获取一下apikey(貌似需要手机验证码认证下)

WeChat public account weather query interface example WeChat public account certification WeChat public account template WeChat public account name

我调用的是历史7天和未来4天的接口,接口地址是:

http:<span>//</span><span>apis.baidu.com/apistore/weatherservice/recentweathers</span>
Copy after login

我将这个天气查询的功能集成在了微信公众平台上,支持未来4天天气查询,包括:当前温度、pm值、风向、风力、最高温度、最低温度、天气状态的查询,效果如下:

WeChat public account weather query interface example WeChat public account certification WeChat public account template WeChat public account name

闲话不多说,我就喜欢来点直接的,核心代码如下:

<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>$icount(<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>
Copy after login

查询格式:“天气 城市名”

以上就介绍了微信公众号天气查询接口实例,包括了微信公众号方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Scrapy implements crawling and analysis of WeChat public account articles Scrapy implements crawling and analysis of WeChat public account articles Jun 22, 2023 am 09:41 AM

Scrapy implements article crawling and analysis of WeChat public accounts. WeChat is a popular social media application in recent years, and the public accounts operated in it also play a very important role. As we all know, WeChat public accounts are an ocean of information and knowledge, because each public account can publish articles, graphic messages and other information. This information can be widely used in many fields, such as media reports, academic research, etc. So, this article will introduce how to use the Scrapy framework to crawl and analyze WeChat public account articles. Scr

What are the differences between WeChat official account certification and non-certification? What are the differences between WeChat official account certification and non-certification? Sep 19, 2023 pm 02:15 PM

The difference between WeChat public account authentication and non-authentication lies in the authentication logo, function permissions, push frequency, interface permissions and user trust. Detailed introduction: 1. Certification logo. Certified public accounts will obtain the official certification logo, which is the blue V logo. This logo can increase the credibility and authority of the public account and make it easier for users to identify the real official public account; 2. Function permissions. Certified public accounts have more functions and permissions than uncertified public accounts. For example, certified public accounts can apply to activate the WeChat payment function to achieve online payment and commercial operations, etc.

Practical crawler combat in Python: WeChat public account crawler Practical crawler combat in Python: WeChat public account crawler Jun 10, 2023 am 09:01 AM

Python is an elegant programming language with powerful data processing and web crawling capabilities. In this digital era, the Internet is filled with a large amount of data, and crawlers have become an important means of obtaining data. Therefore, Python crawlers are widely used in data analysis and mining. In this article, we will introduce how to use Python crawler to obtain WeChat public account article information. WeChat official account is a popular social media platform for publishing articles online and is an important tool for promotion and marketing of many companies and self-media.

How to use Laravel to develop an online ordering system based on WeChat public account How to use Laravel to develop an online ordering system based on WeChat public account Nov 02, 2023 am 09:42 AM

How to use Laravel to develop an online ordering system based on WeChat official accounts. With the widespread use of WeChat official accounts, more and more companies are beginning to use them as an important channel for online marketing. In the catering industry, developing an online ordering system based on WeChat public accounts can improve the efficiency and sales of enterprises. This article will introduce how to use the Laravel framework to develop such a system and provide specific code examples. Project preparation First, you need to ensure that the Laravel framework has been installed in the local environment. OK

Use PHP to build a WeChat public account API interface Use PHP to build a WeChat public account API interface May 13, 2023 pm 12:01 PM

In today's Internet era, WeChat official accounts have become an important marketing channel for more and more companies. If you want your WeChat official account to implement more functions, you often need to write corresponding interfaces. This article will use PHP language as an example to introduce how to build a WeChat public account API interface. 1. Preparation Before writing the WeChat public account API interface, the developer needs to have a WeChat public account and apply for developer interface permissions in the WeChat public platform. After the application is successful, you can obtain the relevant developer AppID and AppSe

Can the official account only post one article per day? Can the official account only post one article per day? Jun 16, 2023 pm 02:04 PM

The public account can not only post one article per day, but can publish up to eight articles at a time. How to publish multiple articles: 1. Click "Material Management" on the left, and then click "New Graphic and Text Material" to start editing. First article; 2. After editing the first article, click the + sign under the first article on the left and click "Graphic Message" to edit the second article; 3. After finishing multiple images and text, click " Save and send in bulk" to complete the publishing of multiple articles.

Build WeChat public account application using Go language framework Build WeChat public account application using Go language framework Jun 04, 2023 am 10:40 AM

With the popularity of the Internet and the widespread use of mobile devices, WeChat official accounts have become an indispensable part of corporate marketing. Through WeChat public accounts, companies can easily interact with users, promote products and services, and increase brand awareness. In order to better develop WeChat public account applications, more and more developers and companies choose to use Go language to build WeChat public account applications. Go language is a programming language developed by Google. Its syntax is concise and suitable for building high-performance, high-concurrency real-time applications. In terms of ease of use and

PHP and WeChat public account development guide PHP and WeChat public account development guide Jun 11, 2023 pm 03:31 PM

With the gradual popularity of WeChat public accounts in social networks, more and more developers have begun to get involved in the field of WeChat public account development. Among them, PHP, as a common back-end programming language, has also begun to be widely used in the development of WeChat public accounts. This article will introduce the basic knowledge and common techniques of PHP in WeChat public account development. 1. Basics of PHP and WeChat public account development WeChat public account development WeChat public account refers to an Internet application based on the WeChat platform, which can provide users with different types of services and content, such as information push

See all articles