本人在SAE环境下搭建了CI框架(其实这个小东西用不着用框架的),直接把代码写在了控制器里面

<"TOKEN", "109");
 
class Welcome extends CI_Controller {
 
  public function index()
  {
    /*
    $this->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 = "<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <Content><![CDATA[%s]]></Content>
                </xml>";
          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 = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[%s]]></MsgType>
                    <ArticleCount>2</ArticleCount>
                     
                    <Articles>
                      <item>
                        <Title><![CDATA[%s]]></Title> 
                        <Description><![CDATA[%s]]></Description>
                        <PicUrl><![CDATA[%s]]></PicUrl>
                        <Url><![CDATA[%s]]></Url>
                      </item>
                      <item>
                        <Title><![CDATA[%s]]></Title>
                        <Description><![CDATA[%s]]></Description>
                        <PicUrl><![CDATA[%s]]></PicUrl>
                        <Url><![CDATA[%s]]></Url>
                      </item>
                    </Articles>
                     
                    </xml> ";
                     
              $resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time,'news',
                '百度','', 'http://www.baidu.com/img/bdlogo.gif', '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;
          $imgTpl = "<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <Image>
                  <MediaId><![CDATA[%s]]></MediaId>
                </Image>
                </xml>";
          $resultStr = sprintf($imgTpl, $fromUsername, $toUsername, $time, 'image', $media_id);
          break;
           
          // try get the id of the receive image and analyse
          /*
          $media_id = $postObj->MediaId;
          $textTpl = "<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <Content><![CDATA[%s]]></Content>
                </xml>";
          $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 = "<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <Content><![CDATA[%s]]></Content>
                </xml>"; 
          $contentStr = '你说啥"Input something...";
          break;
      }
      echo $resultStr;
       
    }else {
      echo "";
      exit;
    }
  }
}

标签:
php,微信

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“微信公众平台DEMO(PHP)”

暂无“微信公众平台DEMO(PHP)”评论...

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?