找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 391|回复: 0
打印 上一主题 下一主题

微信公众平台DEMO(PHP)

[复制链接]

2588

主题

2588

帖子

7694

积分

论坛元老

Rank: 8Rank: 8

积分
7694
跳转到指定楼层
楼主
发表于 2018-2-14 05:36:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            本人在SAE环境下搭建了CI框架(其实这个小东西用不着用框架的),直接把代码写在了控制器里面
load->helper('url');
    $this->load->view('welcome_message');
    */
     
    // use chat response
    $this->responseMsg();
   
  }
   
  // chat response
  public function responseMsg()
  {
    //get post data, May be due to the different environments
    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

     //extract post data
    if (!empty($postStr)){
      
      $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
      $fromUsername = $postObj->FromUserName;
      $toUsername = $postObj->ToUserName;
      $MsgType = $postObj->MsgType;
      $time = time();
      
      switch($MsgType){
        case('text'):
          $keyword = trim($postObj->Content);
          $textTpl = "
               
               
                %s
               
               
                ";
          switch($keyword){
            case(1):
              // Need to optimize
              // random read data from jokes
              $sql = 'SELECT * FROM jokes';
              $query = $this->db->query($sql);
              $res = $query->result_array();
              $num_rows = $query->num_rows();
              $key = rand(0, $num_rows - 1); // Notice: The value of key is from 0.
              //$contentStr = $key.'#'.$res[$key ]['content']; // debug
              $contentStr = $res[$key ]['content'];
              break;
            case(2):
              $contentStr = 'Your fromUsername is: '.$fromUsername;
              break;            
            case(3):
              $newsTpl = "
                    
                    
                    %s
                    
                    2
                     
                    
                     
                        
                        
                        
                        [U][/U]
                     

                     
                        
                        
                        
                        [U][/U]
                     

                    
                     
                     ";
                     
              $resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time,'news',
                '百度','', 'http://www.baidu.com/', 'http://www.baidu.com',
                'Google','', '', 'http://www.google.com'); // Notice: Google's logo is not suitable.
              echo $resultStr;
              exit; // Notice: It's exit, not break.               
            case(4):
              $contentStr = "该功能正在开发中,敬请期待...";
              break;
               
            /* others */
            
            default:
              $contentStr = "回复数字 选择服务\n";
              $contentStr .= "1 笑话精选\n";
              $contentStr .= "2 获取您的Username...\n";  
              $contentStr .= "3 图文消息示例\n";
              $contentStr .= "4 开发中...\n";
              break;
          }
          $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contentStr);
          break;
           
        case('image'):
           
          $media_id = $postObj->MediaId;
          $"
               
               
                %s
               
                [I]
                  
                [/I]
                ";
          $resultStr = sprintf($, $fromUsername, $toUsername, $time, 'image', $media_id);
          break;
           
          // try get the id of the receive image and analyse
          /*
          $media_id = $postObj->MediaId;
          $textTpl = "
               
               
                %s
               
               
                ";
          $length = strlen($media_id);
          $contentStr = "I have received the image message you sent, the id of this image is # $media_id #, and the length of media_id is # $length #";
          $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contentStr);
          break;
          */
           
        case('voice'):
          $textTpl = "
               
               
                %s
               
               
                ";
          $contentStr = '你说啥?俺听不见...';
          $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contentStr);
          break;
         
        /* others */
         
        default:
          $resultStr = "Input something...";
          break;
      }
      echo $resultStr;
      
    }else {
      echo "";
      exit;
    }
  }
}
            
            
您可能感兴趣的文章:
  • 抛弃 PHP 代价太高
  • 100行PHP代码实现socks5代理服务器
  • win7下搭建nginx+php的开发环境
  • PHP.vs.JAVA
  • java解析php函数json_encode unicode 编码问题
  • wdcp添加tomcat,同时支持php和java教程
  • java URL 获取PHP JSON 数据
  • PHPExcel笔记, mpdf导出
  • php面向对象值单例模式
  • PHP中的.htaccess伪静态文件
  • Centos安装php高版本
  • 100多行PHP代码实现socks5代理服务器[2]
  • win10环境PHP 7 安装配置【教程】
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端