微信公眾平台DEMO(PHP),公眾demo_PHP教程

來源:互聯網
上載者:User

公眾平台DEMO(PHP),公眾demo


本人在SAE環境下搭建了CI架構(其實這個小東西用不著用架構的),直接把代碼寫在了控制器裡面

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); //define your tokendefine("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 = "                %s                %s                %s                %s                %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                    %s                    %s                    %s                    2                                                                                       <![CDATA[%s]]>                         %s                        %s                        %s                                                                    <![CDATA[%s]]>                        %s                        %s                        %s                                                                                    ";                                   $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 = "                %s                %s                %s                %s                                  %s                                ";          $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 = "                %s                %s                %s                %s                %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                %s                %s                %s                %s                ";           $contentStr = '你說啥?俺聽不見...';          $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, 'text', $contentStr);          break;                 /* others */                 default:          $resultStr = "Input something...";          break;      }      echo $resultStr;           }else {      echo "";      exit;    }  }}

http://www.bkjia.com/PHPjc/1125233.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1125233.htmlTechArticle公眾平台DEMO(PHP),公眾demo 本人在SAE環境下搭建了CI架構(其實這個小東西用不著用架構的),直接把代碼寫在了控制器裡面 php if ( ! def...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.