如何使用騰訊云云主機搭建微信服務器

首先我們這里要有一臺騰訊云主機,作為學生的我們,說么幸福,一元一個月。參看:


然后購買之后,我們選擇centos系統作為本次的操作的對象,首先我們要搭建一下LAMP

環境,具體操作如下:

(參見我上篇文章《 centos 如何通過yum源搭建lamp環境》)

然后我們在/var/www/html文件下創建一個index.php文件

具體命令:

vi index.php


/**

* wechat php test

*/

//define your token

define("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

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;

}

}

}

?>

然后保存,我們這個時候去微信公共平臺申請一個測試號


在url 填上http://ip? token:weixin

這個時候我們可以點擊提交,如果一次失敗,點擊多次。然后就這樣搭建成功了。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 1、開啟公眾號開發者模式 公眾平臺的技術文檔目的為了簡明扼要的交代接口的使用,語句難免苦澀難懂,甚至對于不同的讀者...
    good7758閱讀 1,549評論 0 1
  • 輸了就是輸了,無論再多說什么,都只是無力的借口。好笑,當自己的能力無法對得起自己的驕傲時,所做出的一切舉動都是那么...
    隋懿閱讀 141評論 0 0
  • 喜樂操練90天——73《一同慶賀!》 一個罪人悔改,在天上也要這樣為他歡喜,較比為九十九個不用悔改的義人歡喜更大。...
    午后的咖啡與茶閱讀 487評論 0 0
  • 1
    why潑灑閱讀 212評論 0 0
  • 今天拿了809的產品,還是在練習洗臉,佳欣姐做了一遍全的,讓我拍視頻照做,不懂的再問她。 晚上的時候來了一個大姐,...
    挽箏閱讀 148評論 0 0