PHP技術(shù)開發(fā)微信公眾平臺(tái)
下面通過圖文并茂的方式介紹微信公眾平臺(tái)開發(fā)過程,具體內(nèi)容如下:
微信公眾平臺(tái)有兩種模式:編輯模式 和 開發(fā)模式。
普通的功能可以通過編輯模式來搞定。開發(fā)模式具有更多的功能。讓我們來使用開發(fā)模式開發(fā)helloword吧
步驟如下:
第一步:先注冊(cè)一個(gè)公眾號(hào)(https://mp.weixin.qq.com)
第二步:注冊(cè)sae(http://sae.sina.com.cn/),作為你的服務(wù)器。
第三步:登錄微信公眾平臺(tái)(https://mp.weixin.qq.com)查看開發(fā)文檔并下載官方提供的demo。做適當(dāng)修改。
第四步:將代碼壓縮成zip格式,上傳到sae平臺(tái)。
第五步:登錄微信公眾平臺(tái),進(jìn)入開發(fā)者中心。開啟“服務(wù)者配置”。
第六步:成功了。
開始吧:
1.先注冊(cè)一個(gè)公眾號(hào)(https://mp.weixin.qq.com)
2.注冊(cè)sae(http://sae.sina.com.cn/)
注冊(cè)完以后要記得進(jìn)行實(shí)名認(rèn)證,不然綁定到公眾平臺(tái)的時(shí)候,會(huì)有永遠(yuǎn)的“無法獲取token”的提示。(實(shí)名認(rèn)證需要3個(gè)工作日才能成功)
然后可以點(diǎn)擊創(chuàng)建應(yīng)用。創(chuàng)建后可以在下面看到。

進(jìn)入自己創(chuàng)建的應(yīng)用。然后點(diǎn)擊代碼管理。


3.登錄微信公眾平臺(tái)(https://mp.weixin.qq.com)
查看開發(fā)文檔并下載官方提供的demo。



打開后是如下的代碼:
<?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 = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
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;
}
}
}
?>
我試過,如上代碼,似乎無法執(zhí)行到response那一塊。所以做了更改
<?php
/**
* wechat php test
*/
//define your token
define("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 = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
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平臺(tái)。

點(diǎn)擊“編輯代碼”,可以看到你上傳的php文件。然后右擊,url查看。復(fù)制url(http://1.carlzhang.sinaapp.com/wx_carlzhang819.php)。在這里要記住在此php文件中定義的token。此處為“weixin”,可以在如下圖中看到。

5.登錄微信公眾平臺(tái),進(jìn)入開發(fā)者中心。開啟“服務(wù)者配置”。url填寫上一步復(fù)制的url(這里我刪除了前面的1.因?yàn)槲业膕ae默認(rèn)第一版。你可以試試,刪除1,若是url訪問,不報(bào)404,那就是沒問題)。token填寫的是代碼中的token(上面是“weixin”)。


如果啟用成功,就可以關(guān)注你的微信平臺(tái),輸入內(nèi)容,看看提示是不是Welcome to wechat world!或者Hello Word!
以上全部內(nèi)容就是針對(duì)微信公眾平臺(tái)做的講解,希望可以幫助到大家。
相關(guān)文章
PHP使用星號(hào)替代用戶名手機(jī)和郵箱的實(shí)現(xiàn)代碼
這篇文章主要介紹了PHP使用星號(hào)替代用戶名手機(jī)和郵箱的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-02-02
thinkPHP多表查詢及分頁功能實(shí)現(xiàn)方法示例
這篇文章主要介紹了thinkPHP多表查詢及分頁功能實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式分析了thinkPHP多表查詢以及查詢結(jié)果的分頁顯示相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-07-07
Yii操作數(shù)據(jù)庫實(shí)現(xiàn)動(dòng)態(tài)獲取表名的方法
這篇文章主要介紹了Yii操作數(shù)據(jù)庫實(shí)現(xiàn)動(dòng)態(tài)獲取表名的方法,涉及Yii框架針對(duì)數(shù)據(jù)庫的動(dòng)態(tài)操作技巧,需要的朋友可以參考下2016-03-03
Ubuntu中啟用php的mail()函數(shù)并解決發(fā)送郵件速度慢問題
本文主要給大家介紹的是在Ubuntu下安裝sendmail的方法,以及啟用sendmail之后,php發(fā)送郵件緩慢的原因及解決方法,有需要的小伙伴可以參考下。2015-03-03
Laravel5.4框架使用socialite實(shí)現(xiàn)github登錄的方法
這篇文章主要介紹了Laravel5.4框架使用socialite實(shí)現(xiàn)github登錄的方法,結(jié)合實(shí)例形式分析了Laravel相關(guān)下載、安裝、配置及github登陸、注冊(cè)、設(shè)置等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
ThinkPHP模板Volist標(biāo)簽嵌套循環(huán)輸出多維數(shù)組的方法
這篇文章主要介紹了ThinkPHP模板Volist標(biāo)簽嵌套循環(huán)輸出多維數(shù)組的方法,結(jié)合實(shí)例形式詳細(xì)分析了Volist標(biāo)簽嵌套調(diào)用輸出多維數(shù)組的相關(guān)技巧,需要的朋友可以參考下2016-03-03
php實(shí)現(xiàn)QQ小程序發(fā)送模板消息功能
QQ小程序群里有伙伴要發(fā)送模板消息的代碼,所以今天給大家分享QQ小程序模板消息發(fā)布,絕對(duì)一步一步帶著大家走,每個(gè)細(xì)節(jié)都講到,感興趣的朋友跟隨小編一起看看吧2019-09-09

