PHP技術開發微信公眾平台_php執行個體

來源:互聯網
上載者:User
下面通過圖文並茂的方式介紹公眾平台開發過程,具體內容如下:

公眾平台有兩種模式:編輯模式 和 開發模式。

普通的功能可以通過編輯模式來搞定。開發模式具有更多的功能。讓我們來使用開發模式開發helloword吧

步驟如下:

第一步:先註冊一個公眾號(https://mp.weixin.qq.com)

第二步:註冊sae(http://sae.sina.com.cn/),作為你的伺服器。

第三步:登入公眾平台(https://mp.weixin.qq.com)查看開發文檔並下載官方提供的demo。做適當修改。

第四步:將代碼壓縮成zip格式,上傳到sae平台。

第五步:登入公眾平台,進入開發人員中心。開啟“服務者配置”。

第六步:成功了。

開始吧:

1.先註冊一個公眾號(https://mp.weixin.qq.com)

2.註冊sae(http://sae.sina.com.cn/)

註冊完以後要記得進行實名認證,不然綁定到公眾平台的時候,會有永遠的“無法擷取token”的提示。(實名認證需要3個工作日才能成功)

然後可以點擊建立應用。建立後可以在下面看到。

進入自己建立的應用。然後點擊代碼管理。

3.登入公眾平台(https://mp.weixin.qq.com)

查看開發文檔並下載官方提供的demo。

開啟後是如下的代碼:

<?php/** * wechat php test *///define your tokendefine("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();$wechatObj->valid();class wechatCallbackapiTest{ public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){  echo $echoStr;  exit; } } 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)){  /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,   the best way is to check the validity of xml by yourself */  libxml_disable_entity_loader(true);   $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);  $fromUsername = $postObj->FromUserName;  $toUsername = $postObj->ToUserName;  $keyword = trim($postObj->Content);  $time = time();  $textTpl = "    %s    %s    %s    %s    %s    0    ";   if(!empty( $keyword ))  {   $msgType = "text";   $contentStr = "Welcome to wechat world!";   $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);   echo $resultStr;  }else{   echo "Input something...";  } }else {  echo "";  exit; } } private function checkSignature() { // you must define TOKEN by yourself if (!defined("TOKEN")) {  throw new Exception('TOKEN is not defined!'); } $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){  return true; }else{  return false; } }}?>

我試過,如上代碼,似乎無法執行到response那一塊。所以做了更改

<?php/** * wechat php test *///define your tokendefine("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();//這裡做了更改if($_GET["echostr"]){ $wechatObj->valid();}else{ $wechatObj->responseMsg();}class wechatCallbackapiTest{ public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){  echo $echoStr;  exit; } } 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)){  /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,   the best way is to check the validity of xml by yourself */  libxml_disable_entity_loader(true);   $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);  $fromUsername = $postObj->FromUserName;  $toUsername = $postObj->ToUserName;  $keyword = trim($postObj->Content);  $time = time();  $textTpl = "    %s    %s    %s    %s    %s    0    ";   if(!empty( $keyword ))  {   $msgType = "text";   $contentStr = "Welcome to wechat world!";   $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);   echo $resultStr;  }else{   echo "Input something...";  } }else {  echo "";  exit; } } private function checkSignature() { // you must define TOKEN by yourself if (!defined("TOKEN")) {  throw new Exception('TOKEN is not defined!'); } $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){  return true; }else{  return false; } }}?>

你可以將Welcome to wechat world!更改為Hello Word!

4.將代碼壓縮成zip格式,上傳到sae平台。

點擊“編輯代碼”,可以看到你上傳的php檔案。然後右擊,url查看。複製url(http://1.carlzhang.sinaapp.com/wx_carlzhang819.php)。在這裡要記住在此php檔案中定義的token。此處為“weixin”,可以在如中看到。

5.登入公眾平台,進入開發人員中心。開啟“服務者配置”。url填寫上一步複製的url(這裡我刪除了前面的1.因為我的sae預設第一版。你可以試試,刪除1,若是url訪問,不報404,那就是沒問題)。token填寫的是代碼中的token(上面是“weixin”)。

如果啟用成功,就可以關注你的平台,輸入內容,看看提示是不是Welcome to wechat world!或者Hello Word!

以上全部內容就是針對公眾平台做的講解,希望可以協助到大家。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.