微信公众平台接口开发:(4)天气预报

移动开发
本系列教程是微信公众平台开发的高级篇,以微信公众平台应用天气神(WeatherGod)为例,讲解微信接口开发过程。本文为第四篇,天气预报功能的实现。

一、请求数据

首先需要能有取得天气数据的接口,这样的接口网上有很多。比如google, yahoo,天气网都提供天气接口

方倍工作室的API已经能提供全国各地的天气预报,使用方倍的API无需再建立城市对应关系表

使用方式为直接在URL中提交城市名称即可,如果找不到城市名称,请先做urlencode

调用url方法:

http://api2.sinaapp.com/search/weather/?appkey=0020130430&appsecert=fa6095e113cd28fd&reqtype=text&keyword=上海 
//或者做一次urlencode后再提交 
http://api2.sinaapp.com/search/weather/?appkey=0020130430&appsecert=fa6095e113cd28fd&reqtype=text&keyword=%E6%B7%B1%E5%9C%B3 
  • 1.
  • 2.
  • 3.

返回格式如下:(返回中自带换行\n操作,不用自己再添加)


    "errcode""0"
    "msgtype""text"
    "text": { 
        "content""【湘潭】天气实况 温度:12℃ 湿度:59% 风速:西北风3级\n03月10日 周日 10℃~21℃ 阴转多云 北风转南风小于3级\n03月11日 周一 13℃~28℃ 多云 南风小于3级\n03月12日 周二 10℃~22℃ 小雨转阴 北风小于3级\n技术支持 方倍工作室" 
    } 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

一个完整的请求类似如下:

$url = "http://api2.sinaapp.com/search/weather/?appkey=0020130430&appsecert=fa6095e113cd28fd&reqtype=text&keyword=%E6%B7%B1%E5%9C%B3"
$weatherJson = file_get_contents($url); 
$weather = json_decode($weatherJson, true); 
$contentStr = $weather['text']['content']; 
  • 1.
  • 2.
  • 3.
  • 4.

如果使用城市名+天气方式查询,则先进行城市名提取:

else if (substr($keyword, -6, strlen($keyword)) == "天气"){ 
    $cityname = trim(substr($keyword, 0, strlen($keyword) - 6)); 
  • 1.
  • 2.

二,效果演示

使用城市名称查询天气预报:

原文链接。本文为方倍工作室原创,51CTO授权转载,如需转载请联系xuchuan(at)51cto.com

完整源代码见第二页。

#p#

二、完整源代码

<?php 
/* 
【版权声明】 
    本软件产品的版权归方倍工作室所有,受《中华人民共和国计算机软件保护条例》等知识产权法律及国际条约与惯例的保护。您获得的只是本软件的使用权。 
 
    您不得: 
    * 在未得到授权的情况下删除、修改本软件及其他副本上一切关于版权的信息; 
    * 销售、出租此软件产品的任何部分; 
    * 从事其他侵害本软件版权的行为。 
 
    如果您未遵守本条款的任一约定,方倍工作室有权立即终止本条款的执行,且您必须立即终止使用本软件并销毁本软件产品的任何副本。这项要求对各种拷贝形式有效。 
 
    您同意承担使用本软件产品的风险,在适用法律允许的最大范围内,方倍工作室在任何情况下不就因使用或不能使用本软件产品所发生的特殊的、意外的、非直接或间接的损失承担赔偿责任。即使已事先被告知该损害发生的可能性。 
 
    如使用本软件所添加的任何信息,发生版权纠纷,方倍工作室不承担任何责任。 
 
    方倍工作室对本条款拥有最终解释权。 
 
    CopyRight 2013  www.doucube.com  All Rights Reserved 
 
*/ 
 
date_default_timezone_set('Asia/Hong_Kong');  //set time zone 
define("TOKEN""http://www.doucube.com"); 
 
$wechatObj = new wechatCallbackapiTest(); 
$wechatObj->responseMsg(); 
 
class wechatCallbackapiTest 

    public function responseMsg() 
    { 
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 
        logger("R ".$postStr); 
        //extract post data 
        if (!emptyempty($postStr)){ 
            $postObj = simplexml_load_string($postStr'SimpleXMLElement', LIBXML_NOCDATA); 
            $RX_TYPE = trim($postObj->MsgType); 
 
            switch ($RX_TYPE
            { 
                case "text"
                    $resultStr = $this->receiveText($postObj); 
                    break
                case "event"
                    $resultStr = $this->receiveEvent($postObj); 
                    break
            } 
            logger("T ".$resultStr); 
            echo $resultStr
        }else { 
            echo ""
            exit
        } 
    } 
 
    private function receiveText($object
    { 
        $funcFlag = 0; 
        $keyword = trim($object->Content); 
        $resultStr = ""
        $contentStr = ""
         
        if (substr($keyword, -6, strlen($keyword)) == "天气"){ 
            $keyword = trim(substr($keyword, 0, strlen($keyword) - 6)); 
            if ($keyword == ""){$keyword = "北京";} 
            $apihost = "http://api2.sinaapp.com/"
            $apimethod = "search/weather/?"
            $apiparams = array('appkey'=>"0020120430"'appsecert'=>"fa6095e113cd28fd"'reqtype'=>"text"); 
            $apikeyword = "&keyword=".urlencode($keyword); 
            $apicallurl = $apihost.$apimethod.http_build_query($apiparams).$apikeyword
            $weatherJson = file_get_contents($apicallurl); 
            $weather = json_decode($weatherJson, true); 
            $contentStr = $weather['text']['content']; 
            $resultStr = $this->transmitText($object$contentStr$funcFlag); 
        }else
            $contentStr = "发送城市加天气的命令查询天气,如“北京天气”,“上海天气”。"
            $resultStr = $this->transmitText($object$contentStr$funcFlag); 
        } 
        return $resultStr
    } 
 
    private function receiveEvent($object
    { 
        $contentStr = ""
        switch ($object->Event) 
        { 
            case "subscribe"
                $contentStr = "Power By 方倍工作室!"
                break
        } 
        $resultStr = $this->transmitText($object$contentStr); 
        return $resultStr
    } 
     
    private function transmitText($object$content$flag = 0) 
    { 
        $textTpl = "<xml> 
<ToUserName><![CDATA[%s]]></ToUserName> 
<FromUserName><![CDATA[%s]]></FromUserName> 
<CreateTime>%s</CreateTime> 
<MsgType><![CDATA[text]]></MsgType> 
<Content><![CDATA[%s]]></Content> 
<FuncFlag>%d</FuncFlag> 
</xml>"; 
        $resultStr = sprintf($textTpl$object->FromUserName, $object->ToUserName, time(), $content$flag); 
        return $resultStr
    } 
 

 
function logger($log_content

    if (isset($_SERVER['HTTP_APPNAME'])){ //SAE 
        sae_set_display_errors(false); 
        sae_debug($log_content); 
        sae_set_display_errors(true); 
    }else { 
        $max_size = 100000; 
        $log_filename = date("Ymd").".xml"
        if(file_exists($log_filenameand (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);} 
        file_put_contents($log_filenamedate('H:i:s')." ".$log_content."\r\n", FILE_APPEND); 
    } 

?> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.

 

责任编辑:徐川 来源: 方倍工作室
相关推荐

2016-03-14 10:29:38

天气预报各类工具源码

2013-04-10 16:15:40

微信公众平台接口开发

2013-03-26 13:20:27

Android天气预报

2013-04-10 18:45:52

微信公众平台接口开发

2009-07-07 09:25:08

Linux开发FOSS开发项目

2013-04-10 18:07:08

微信公众平台接口开发

2013-04-10 18:19:40

微信公众平台接口开发

2021-02-07 09:17:24

鸿蒙HarmonyOS应用开发

2013-04-10 18:29:09

微信公众平台接口开发

2013-04-10 18:24:48

微信公众平台接口开发

2014-11-20 09:38:40

C#

2010-08-13 10:56:58

FlexWebservice

2017-08-01 10:10:32

人工智能智能天气预报

2013-04-15 16:56:48

微信公众平台Android开发

2013-04-10 16:51:56

微信公众平台接口开发

2013-09-09 10:52:10

2013-11-13 07:19:19

2012-06-18 15:40:32

jQuery

2022-02-21 11:02:54

5G通信网络天气预报

2009-12-02 15:45:04

PHP抓取天气预报
点赞
收藏

51CTO技术栈公众号