zoukankan      html  css  js  c++  java
  • 微信公众平台开发(96) 多个功能整合

    思路:

    通过用户发送的关键字判断的方式来判断功能,再调用回复相应的内容。
    当一个功能不匹配的时候,则进入下一个功能判断。

    程序示例如下:

    //接收文本消息
    private function receiveText($object)
    {
        $keyword = trim($object->Content);
        //判断天气
        if (strstr($keyword, "天气")){
            $city = str_replace('天气', '', $keyword);
            include("weather.php");
            $content = getWeatherInfo($city);
        //判断笑话
        }else if (strstr($keyword, "笑话")){
            include("joke.php");
            $content = getJokeInfo();
        //判断世界杯
        }else if (strstr($keyword, "世界杯")){
            $content[] = array("Title" =>"2014年巴西世界杯赛程","Description" =>"", "PicUrl" =>"https://images0.cnblogs.com/i/340216/201406/111304544204656.jpg", "Url" =>"http://url.cn/RInu1v");
        //其他默认回复
        }else{
            $content = date("Y-m-d H:i:s",time())."
    技术支持 方倍工作室";
        }
        
        if(is_array($content)){
            if (isset($content[0]['PicUrl'])){
                $result = $this->transmitNews($object, $content);
            }else if (isset($content['MusicUrl'])){
                $result = $this->transmitMusic($object, $content);
            }
        }else{
            $result = $this->transmitText($object, $content);
        }
    
        return $result;
    }

    上述代码使用if else if这样的分支语句实现类别区分,比如发送“深圳天气”之后,

    if (strstr($keyword, "天气")){

    判断文字中包括“天气”二个字,就进入了天气类别,

    剩下还要把“深圳”2个字提取出来,使用字符串替换的方式,把“天气”2个字替换成空(也就是相当于删除)。

    $city = str_replace('天气', '', $keyword);

    这样就得到城市名称了。

    再将天气查询文件包含进来,并且将城市名称传入

    include("weather.php");
    $content = getWeatherInfo($city);

    这样就查询到了深圳的天气预报信息。

    同样的方法,可以将其他功能完整整合进来。 

  • 相关阅读:
    ASP.NET备份还原数据库
    ASP.NET的运行原理与运行机制
    Asp.net WebPages框架运行原理浅析(转)
    不要盲目选择定时器
    C# 如何用计时器Timer控件实现停留几秒再做切换窗体的操作
    使用System.Timers.Timer类实现程序定时执行
    C#启动一个外部程序(1)-WinExec
    几种类型的db,以及最新的db排名,看一下
    SharePoint 2013 讨论板列表"Connect to Outlook" 不可用解决方案
    Java可视化编程,基于布局管理器的UI设计
  • 原文地址:https://www.cnblogs.com/txw1958/p/weixin96-multi-function.html
Copyright © 2011-2022 走看看