zoukankan      html  css  js  c++  java
  • 微信开发简单实例

    本文主要包括以下内容
    1. 微信聊天机器人

    微信聊天机器人

    利用图灵机器人接口实现微信聊天机器人

    <?php
    
    define("TOKEN", "weixin");
    $wechatObj = new wechatCallbackapiTest();
    if (isset($_GET['echostr'])) {
        $wechatObj->valid();
    }else{
        $wechatObj->responseMsg();
    }
    
    class wechatCallbackapiTest
    {
        public function valid()
        {
            $echoStr = $_GET["echostr"];
            if($this->checkSignature()){
                header('content-type:text');
                echo $echoStr;
                exit;
            }
        }
    
        private function checkSignature()
        {
            $signature = $_GET["signature"];
            $timestamp = $_GET["timestamp"];
            $nonce = $_GET["nonce"];
    
            $token = TOKEN;
            $tmpArr = array($token, $timestamp, $nonce);
            sort($tmpArr, SORT_STRING);
            $tmpStr = implode( $tmpArr );
            $tmpStr = sha1( $tmpStr );
    
            if( $tmpStr == $signature ){
                return true;
            }else{
                return false;
            }
        }
    
        public function responseMsg()
        {
            $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
    
            if (!empty($postStr)){
                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                $textTpl = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[%s]]></MsgType>
                            <Content><![CDATA[%s]]></Content>
                            <FuncFlag>0</FuncFlag>
                            </xml>";
                // if($keyword == "?" || $keyword == "?")
                // {
                //     $msgType = "text";
                //     $contentStr = date("Y-m-d H:i:s",time());
                //     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                //     echo $resultStr;
                // }
                $msgType="text";
                $resulttalk=$this->tuling($keyword);
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $resulttalk);
                echo $resultStr;
            }else{
                echo "";
                exit;
            }
        }
    
            // 图灵机器人
        function tuling($keyword) {
            $key="82afb9ed426e8506be0fef7f70ed3f16";//api key到这里申请
            $api_url = "http://www.tuling123.com/openapi/api?key=".$key."&info=". $keyword;
    
            $result = file_get_contents ( $api_url );
            $result = json_decode ( $result, true );
    
            switch ($result ['code']) {
                case '100000':
                    $resulttext=$result['text'];
                    return $resulttext;
                    break;
              case '200000' :
                $text = $result ['text'] . ',<a href="' . $result ['url'] . '">点击进入</a>';
                return $text;
                break;
              case '301000' :
            $length = count($result['list']) > 9 ? 9 :count($result['list']);
                for($i= 0;$i<$length;$i++){
                    $articles [$i] = array (
                        'Title' => $result['list'][$i]['name'],
                        'Description' => $result['list'][$i]['author'],
                        'PicUrl' => $result['list'][$i]['icon'],
                        'Url' => $result['list'][$i]['detailurl']
                    );
                }
                return $articles;
                break;
              case '302000' :
                $length = count($result['list']) > 9 ? 9 :count($result['list']);
                for($i= 0;$i<$length;$i++){
                    $articles [$i] = array (
                        'Title' => $result['list'][$i]['article'],
                        'Description' => $result['list'][$i]['source'],
                        'PicUrl' => $result['list'][$i]['icon'],
                        'Url' => $result['list'][$i]['detailurl']
                    );
                }
                return $articles;
                break;
              case '304000' :
                $length = count($result['list']) > 9 ? 9 :count($result['list']);
                for($i= 0;$i< $length;$i++){
                    $articles [$i] = array (
                        'Title' => $result['list'][$i]['name'],
                        'Description' => $result['list'][$i]['count'],
                        'PicUrl' => $result['list'][$i]['icon'],
                        'Url' => $result['list'][$i]['detailurl']
                    );
                }
                return $articles;
                break;
              case '305000' :
                $length = count($result['list']) > 9 ? 9 :count($result['list']);
                for($i= 0;$i<$length;$i++){
                    $articles [$i] = array (
                        'Title' => $result['list'][$i]['start'] . '--' . $result['list'][$i]['terminal'],
                        'Description' => $result['list'][$i]['starttime'] . '--' . $result['list'][$i]['endtime'],
                        'PicUrl' => $result['list'][$i]['icon'],
                        'Url' => $result['list'][$i]['detailurl']
                    );
                }
                return $articles;
                break;
              case '306000' :
                $length = count($result['list']) > 9 ? 9 :count($result['list']);
                for($i= 0;$i<$length;$i++){
                    $articles [$i] = array (
                        'Title' => $result['list'][$i]['flight'] . '--' . $result['list'][$i]['route'],
                        'Description' => $result['list'][$i]['starttime'] . '--' . $result['list'][$i]['endtime'],
                        'PicUrl' => $result['list'][$i]['icon'],
                        'Url' => $result['list'][$i]['detailurl']
                    );
                }
                return $articles;
                break;
              case '307000' :
                $length = count($result['list']) > 9 ? 9 :count($result['list']);
                for($i= 0;$i<$length;$i++){
                    $articles [$i] = array (
                        'Title' => $result['list'][$i]['name'],
                        'Description' => $result['list'][$i]['info'],
                        'PicUrl' => $result['list'][$i]['icon'],
                        'Url' => $result['list'][$i]['detailurl']
                    );
                }
                return $articles;
                break;
              case '308000' :
                $length = count($result['list']) > 9 ? 9 :count($result['list']);
                for($i= 0;$i<$length;$i++){
                    $articles [$i] = array (
                        'Title' => $result['list'][$i]['name'],
                        'Description' => $result['list'][$i]['info'],
                        'PicUrl' => $result['list'][$i]['icon'],
                        'Url' => $result['list'][$i]['detailurl']
                    );
                }
                return $articles;
                break;
              case '309000' :
                $length = count($result['list']) > 9 ? 9 :count($result['list']);
                for($i= 0;$i<$length;$i++){
                    $articles [$i] = array (
                        'Title' => $result['list'][$i]['name'],
                        'Description' => '价格 : ' . $result['list'][$i]['price'] . ' 满意度 : ' . $result['list']['satisfaction'],
                        'PicUrl' => $result['list'][$i]['icon'],
                        'Url' => $result['list'][$i]['detailurl']
                    );
                }
                return $articles;
                break;
              case '310000' :
                $length = count($result['list']) > 9 ? 9 :count($result['list']);
                for($i= 0;$i<$length;$i++){
                    $articles [$i] = array (
                        'Title' => $result['list'][$i]['number'],
                        'Description' => $result['list'][$i]['info'],
                        'PicUrl' => $result['list'][$i]['icon'],
                        'Url' => $result['list'][$i]['detailurl']
                    );
                }
                return $articles;
                break;
              case '311000' :
                $length = count($result['list']) > 9 ? 9 :count($result['list']);
                for($i= 0;$i<$length;$i++){
                    $articles [$i] = array (
                        'Title' => $result['list'][$i]['name'],
                        'Description' => '价格 : ' . $result['list'][$i]['price'],
                        'PicUrl' => $result['list'][$i]['icon'],
                        'Url' => $result['list'][$i]['detailurl']
                    );
                }
                return $articles;
                break;
              case '312000' :
                $length = count($result['list']) > 9 ? 9 :count($result['list']);
                for($i= 0;$i<$length;$i++){
                    $articles [$i] = array (
                        'Title' => $result['list'][$i]['name'],
                        'Description' => '价格 : ' . $result['list'][$i]['price'],
                        'PicUrl' => $result['list'][$i]['icon'],
                        'Url' => $result['list'][$i]['detailurl']
                    );
                }
                return $articles;
                break;
              default :
                if (emptyempty ( $result ['text'] )) {
                    return false;
                } else {
                    return $result ['text'] ;
                }
            }
    
        } 
    
    
    }
    ?>

  • 相关阅读:
    微软ASP.NET网站部署指南(4):配置项目属性
    iOS 设计模式之抽象工厂
    How can I move a MySQL database from one server to another?
    CentOS 7 上安装vim(默认未安装)
    How to resize slide dimensions without resizing any objects on the slide?
    CentOS 7.3 上安装docker
    美国留学访学(访问学者)必备信用卡
    西安理工大学税务登记证、银行账号信息
    Oracle联合多个子查询(inner join)
    linux tail
  • 原文地址:https://www.cnblogs.com/jjx2013/p/6223591.html
Copyright © 2011-2022 走看看