《PHP應用:PHP技術開發微信公眾平臺》要點:
本文介紹了PHP應用:PHP技術開發微信公眾平臺,希望對您有用。如果有疑問,可以聯系我們。
PHP教程下面通過圖文并茂的方式介紹微信公眾平臺開發過程,具體內容如下:
PHP教程微信公眾平臺有兩種模式:編輯模式 和 開發模式.
PHP教程普通的功能可以通過編輯模式來搞定.開發模式具有更多的功能.讓我們來使用開發模式開發helloword吧
PHP教程步驟如下:
PHP教程第一步:先注冊一個公眾號(https://mp.weixin.qq.com)
PHP教程第二步:注冊sae(http://sae.sina.com.cn/),作為你的服務器.
PHP教程第三步:登錄微信公眾平臺(https://mp.weixin.qq.com)查看開發文檔并下載官方提供的demo.做適當修改.
PHP教程第四步:將代碼壓縮成zip格式,上傳到sae平臺.
PHP教程第五步:登錄微信公眾平臺,進入開發者中心.開啟“服務者配置”.
PHP教程第六步:成功了.
PHP教程開始吧:
PHP教程1.先注冊一個公眾號(https://mp.weixin.qq.com)
PHP教程2.注冊sae(http://sae.sina.com.cn/)
PHP教程注冊完以后要記得進行實名認證,不然綁定到公眾平臺的時候,會有永遠的“無法獲取token”的提示.(實名認證需要3個工作日才能成功)
PHP教程然后可以點擊創建應用.創建后可以在下面看到.
PHP教程
PHP教程進入自己創建的應用.然后點擊代碼管理.
PHP教程
PHP教程
PHP教程3.登錄微信公眾平臺(https://mp.weixin.qq.com)
PHP教程查看開發文檔并下載官方提供的demo.
PHP教程
PHP教程
PHP教程
PHP教程打開后是如下的代碼:
PHP教程
<?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;
}
}
}
?>
PHP教程我試過,如上代碼,似乎無法執行到response那一塊.所以做了更改
PHP教程
<?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;
}
}
}
?>
PHP教程你可以將Welcome to wechat world!更改為Hello Word!
PHP教程4.將代碼壓縮成zip格式,上傳到sae平臺.
PHP教程
PHP教程點擊“編輯代碼”,可以看到你上傳的php文件.然后右擊,url查看.復制url(http://1.carlzhang.sinaapp.com/wx_carlzhang819.php).在這里要記住在此php文件中定義的token.此處為“weixin”,可以在如下圖中看到.
PHP教程
PHP教程5.登錄微信公眾平臺,進入開發者中心.開啟“服務者配置”.url填寫上一步復制的url(這里我刪除了前面的1.因為我的sae默認第一版.你可以試試,刪除1,若是url拜訪,不報404,那就是沒問題).token填寫的是代碼中的token(上面是“weixin”).
PHP教程
PHP教程
PHP教程如果啟用成功,就可以關注你的微信平臺,輸入內容,看看提示是不是Welcome to wechat world!或者Hello Word!
PHP教程以上全部內容就是針對微信公眾平臺做的講解,希望可以贊助到大家.
維易PHP培訓學院每天發布《PHP應用:PHP技術開發微信公眾平臺》等實戰技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養人才。