


WeChat public account weather query interface example, public weather query example_PHP tutorial
微信公众号天气查询接口实例,公众天气查询实例
这是来自百度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>
查询格式:“天气 城市名”

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.

The session ID should be regenerated regularly at login, before sensitive operations, and every 30 minutes. 1. Regenerate the session ID when logging in to prevent session fixed attacks. 2. Regenerate before sensitive operations to improve safety. 3. Regular regeneration reduces long-term utilization risks, but the user experience needs to be weighed.

Setting session cookie parameters in PHP can be achieved through the session_set_cookie_params() function. 1) Use this function to set parameters, such as expiration time, path, domain name, security flag, etc.; 2) Call session_start() to make the parameters take effect; 3) Dynamically adjust parameters according to needs, such as user login status; 4) Pay attention to setting secure and httponly flags to improve security.

The main purpose of using sessions in PHP is to maintain the status of the user between different pages. 1) The session is started through the session_start() function, creating a unique session ID and storing it in the user cookie. 2) Session data is saved on the server, allowing data to be passed between different requests, such as login status and shopping cart content.

How to share a session between subdomains? Implemented by setting session cookies for common domain names. 1. Set the domain of the session cookie to .example.com on the server side. 2. Choose the appropriate session storage method, such as memory, database or distributed cache. 3. Pass the session ID through cookies, and the server retrieves and updates the session data based on the ID.


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

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

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.