使用新浪雲(SAE)做爲微信公衆號服務器首先須要有一個新浪微博帳號,沒有的話,php
請先註冊一個,而後開始下面的步驟:html
1 點擊下面的地址,使用新浪微博帳號登陸,申請新浪雲服務器資源。git
2 點擊頂部左上角菜單"控制檯",在下拉菜單中選擇"雲應用 SAE",進入下面的頁面:api
點擊左下角"建立新應用"按鈕,在彈出的"SAE提醒您"窗口中,點擊"繼續建立"。服務器
3 在彈出的窗口中,填寫域名,應用名稱,以及開發語言等:微信
例如,二級域名:weixinkaifa 應用名稱:微信公衆號開發測試 開發語言:PHP 5.6,而後選擇空應用,完成應用建立。app
4 完成建立後,會自動跳轉到應用列表,能夠看到剛纔建立的應用,以下圖:微信公衆平臺
請記住"應用信息"欄中,"微信公衆號開發測試"下面的連接,這裏爲"http://weixinkaifa.applinzi.com"post
5 點擊應用"微信公衆號開發測試", 進入"應用主頁", 在左側菜單"數據庫服務"的下拉菜單中選擇"獨享型MySQL"
以下圖:
點擊"確認初始化", 初始化完成後,再點擊"銷燬MySQL"按鈕, 以下圖:
這樣先建立獨享型MySQL,是由於第一次使用獨享型MySQL,獎勵200雲豆,而再銷燬是由於獨享型雲豆比較貴,銷燬完獨享型MySql後,再按步驟5,從新建立一次數據庫,這一次不須要再銷燬。
6 在左側菜單"應用"的下拉菜單中選擇"代碼管理",以下圖:
選擇使用"SVN"進行代理管理,建立完成後,會看到以下界面:
到這裏就完成了一個應用的建立,再次強調,請記住第4步中的URL,這個URL將做爲咱們本身的微信公衆號服務器。
7 將下面的代碼用utf-8格式保存,再壓縮成index.zip文件,點擊第6步圖中的"上傳代碼",上傳成功後,點擊第6步
圖中的"編輯代碼",能夠查看上傳的代碼。
聲明:如下代碼來自"方倍工做室"的博文,非本人原創,博文連接:
http://www.cnblogs.com/txw1958/p/wechat-tutorial.html
如下代碼經本人驗證過可用,若是如下代碼不可用,可到上面的地址上下載完整源碼包index.zip
<?php /* 方倍工做室 http://www.cnblogs.com/txw1958/ CopyRight 2014 All Rights Reserved */ define("TOKEN", "weixinkaifa"); $wechatObj = new wechatCallbackapiTest(); if (!isset($_GET['echostr'])) { $wechatObj->responseMsg(); }else{ $wechatObj->valid(); } class wechatCallbackapiTest { //驗證簽名 public function valid() { $echoStr = $_GET["echostr"]; $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); if($tmpStr == $signature){ header('content-type:text'); echo $echoStr; exit; } } //響應消息 public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $this->logger("R ".$postStr); $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); //消息類型分離 switch ($RX_TYPE) { case "event": $result = $this->receiveEvent($postObj); break; case "text": $result = $this->receiveText($postObj); break; default: $result = "unknown msg type: ".$RX_TYPE; break; } $this->logger("T ".$result); echo $result; }else { echo ""; exit; } } //接收事件消息 private function receiveEvent($object) { $content = ""; switch ($object->Event) { case "subscribe": $content = "歡迎關注方倍工做室 "; $content .= (!empty($object->EventKey))?("\n來自二維碼場景 ".str_replace("qrscene_","",$object->EventKey)):""; break; case "unsubscribe": $content = "取消關注"; break; case "SCAN": $content = "掃描場景 ".$object->EventKey; break; case "CLICK": switch ($object->EventKey) { case "COMPANY": $content = array(); $content[] = array("Title"=>"多圖文1標題", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); break; default: $content = "點擊菜單:".$object->EventKey; break; } break; case "LOCATION": $content = "上傳位置:緯度 ".$object->Latitude.";經度 ".$object->Longitude; break; case "VIEW": $content = "跳轉連接 ".$object->EventKey; break; case "MASSSENDJOBFINISH": $content = "消息ID:".$object->MsgID.",結果:".$object->Status.",粉絲數:".$object->TotalCount.",過濾:".$object->FilterCount.",發送成功:".$object->SentCount.",發送失敗:".$object->ErrorCount; break; default: $content = "receive a new event: ".$object->Event; break; } if(is_array($content)){ if (isset($content[0])){ $result = $this->transmitNews($object, $content); }else if (isset($content['MusicUrl'])){ $result = $this->transmitMusic($object, $content); } }else{ $result = $this->transmitText($object, $content); } return $result; } //接收文本消息 private function receiveText($object) { $keyword = trim($object->Content); //多客服人工回覆模式 if (strstr($keyword, "您好") || strstr($keyword, "你好") || strstr($keyword, "在嗎")){ $result = $this->transmitService($object); } //自動回覆模式 else{ if (strstr($keyword, "文本")){ $content = "這是個文本消息"; }else if (strstr($keyword, "單圖文")){ $content = array(); $content[] = array("Title"=>"單圖文標題", "Description"=>"單圖文內容", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); }else if (strstr($keyword, "圖文") || strstr($keyword, "多圖文")){ $content = array(); $content[] = array("Title"=>"多圖文1標題", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); $content[] = array("Title"=>"多圖文2標題", "Description"=>"", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); $content[] = array("Title"=>"多圖文3標題", "Description"=>"", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958"); }else if (strstr($keyword, "音樂")){ $content = array(); $content = array("Title"=>"最炫民族風", "Description"=>"歌手:鳳凰傳奇", "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3", "HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3"); }else{ $content = date("Y-m-d H:i:s",time())."\n".$object->FromUserName."\n技術支持 方倍工做室"; } if(is_array($content)){ if (isset($content[0]['PicUrl'])){ $result = $this->transmitNews($object, $content); }else if (isset($content['MusicUrl'])){ $result = $this->transmitMusic($object, $content); } }else{ $result = $this->transmitText($object, $content); } } return $result; } //回覆文本消息 private function transmitText($object, $content) { $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content); return $result; } //回覆語音消息 private function transmitVoice($object, $voiceArray) { $itemTpl = "<Voice> <MediaId><![CDATA[%s]]></MediaId> </Voice>"; $item_str = sprintf($itemTpl, $voiceArray['MediaId']); $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[voice]]></MsgType> $item_str </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } //回覆圖文消息 private function transmitNews($object, $newsArray) { if(!is_array($newsArray)){ return; } $itemTpl = " <item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> "; $item_str = ""; foreach ($newsArray as $item){ $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']); } $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>%s</ArticleCount> <Articles> $item_str</Articles> </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray)); return $result; } //回覆音樂消息 private function transmitMusic($object, $musicArray) { $itemTpl = "<Music> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <MusicUrl><![CDATA[%s]]></MusicUrl> <HQMusicUrl><![CDATA[%s]]></HQMusicUrl> </Music>"; $item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']); $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[music]]></MsgType> $item_str </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } //回覆多客服消息 private function transmitService($object) { $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[transfer_customer_service]]></MsgType> </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time()); return $result; } //日誌記錄 private function logger($log_content) { if(isset($_SERVER['HTTP_APPNAME'])){ //SAE sae_set_display_errors(false); sae_debug($log_content); sae_set_display_errors(true); }else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL $max_size = 10000; $log_filename = "log.xml"; if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);} file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND); } } } ?>
8 進入微信公衆平臺地址: https://mp.weixin.qq.com, 在左側菜單中選擇"開發"->"基本配置",點擊
"修改配置"按鈕,在基本配置頁面中填寫URL, Token, EncodingAESKey和消息加解密方式。
URL: 填寫第4步中的URL, 這裏爲"http://weixinkaifa.applinzi.com"
Token:在第7步的代碼中第一行定義了全局變量TOKEN,這裏Token要填的值要和代碼中定義的值一致
define("TOKEN", "weixinkaifa");
EncodingAESKey: 隨機生成,不須要填寫
消息加解密方式:選擇"明文模式"便可。
9 點擊"提交"按鈕,點擊"肯定"按鈕,配置成功後,回到"基本配置"頁面,點擊"啓用"按鈕,啓用成功後,配置就完成了。
測試:打開你的微信公衆號,發送"?",看看會不會回覆當前時間,若是回覆,就說明成功了,之後就可使用
"http://weixinkaifa.applinzi.com" 做爲微信公衆號服務器進行公衆號開發和測試了。