微信公众平台DEMO(PHP)

前端之家收集整理的这篇文章主要介绍了微信公众平台DEMO(PHP)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

PHP;"> //define your token
define("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
          $<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a> = 'SELECT * FROM jokes';
          $query = $this->db->query($<a href="https://www.jb51.cc/tag/sql/" target="_blank" class="keywords">sql</a>);
          $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','<a href="https://www.jb51.cc/tag/baidu/" target="_blank" class="keywords">百度</a>','','http://www.baidu.com/img/bd<a href="https://www.jb51.cc/tag/logo/" target="_blank" class="keywords">logo</a>.gif','http://www.baidu.com','Google','http://www.google.com'); // Notice: Google's <a href="https://www.jb51.cc/tag/logo/" target="_blank" class="keywords">logo</a> is not suitable.
          echo $resultStr;
          exit; // Notice: It's exit,not break.                
        case(4):
          $contentStr = "该<a href="https://www.jb51.cc/tag/gongneng/" target="_blank" class="keywords">功能</a>正在开发中,敬请期待..."; 
          break;

        /* others */

        default:
          $contentStr = "<a href="https://www.jb51.cc/tag/huifu/" target="_blank" class="keywords">回复</a>数字 选择服务\n";
          $contentStr .= "1 笑话精选\n";
          $contentStr .= "2 <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>您的Username...\n";  
          $contentStr .= "3 图文消息示例\n"; 
          $contentStr .= "4 开发中...\n"; 
          break;
      }
      $resultStr = sprintf($textTpl,'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,'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,$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 = '你说啥?俺听不见...';
      $resultStr = sprintf($textTpl,$contentStr);
      break;

    /* others */

    default:
      $resultStr = "Input something...";
      break;
  }
  echo $resultStr;

}else {
  echo "";
  exit;
}

}
}

原文链接:https://www.f2er.com/php/19878.html

猜你在找的PHP相关文章