基于SAE与PHP开发微信公众平台Helloworld教程
以下将通过图文详解的方式,逐步展示微信公众平台的具体开发流程。
微信公众平台主要提供两种运行模式:编辑模式与开发模式。
常规的基础功能通常可通过编辑模式直接配置完成,而开发模式则支持更丰富的交互能力。接下来,我们将利用开发模式来构建一个基础的Hello World示例。
具体的实施步骤如下所示:
第一步:访问微信公众平台官网(https://mp.weixin.qq.com)并完成公众号的注册。
第二步:前往新浪云应用服务(SAE)平台(http://sae.sina.com.cn/)进行注册,将其作为应用的托管服务器。
第三步:重新登录微信公众平台,查阅相关开发文档,并下载官方提供的示例代码(Demo)。根据实际需求对代码进行适当调整。
第四步:将修改后的代码打包压缩为zip格式,随后上传至SAE平台。
第五步:再次登录微信公众平台,进入“开发者中心”界面,启用“服务器配置”功能。
第六步:若配置无误,即表示开发环境搭建成功。
具体操作指南如下:
1. 首先,在微信公众平台官网(https://mp.weixin.qq.com)注册一个公众号。
2. 接着,在新浪云应用服务平台(http://sae.sina.com.cn/)完成账号注册。
注册完成后务必进行实名认证,否则在绑定到公众平台时,可能会遇到无法获取token的错误提示。(注:实名认证通常需要3个工作日才能审核通过。)
认证通过后,即可点击创建应用。创建成功后,可以在列表中看到该应用。

进入刚创建的应用详情页面,然后点击“代码管理”选项卡。


3. 登录微信公众平台(https://mp.weixin.qq.com)。
查看开发文档,并下载官方提供的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;
}
}
}
?>
经测试,上述原始代码似乎无法正确执行到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平台。

点击“编辑代码”,即可看到你上传的php文件。右键点击该文件选择“查看url”,复制生成的访问地址(例如:http://1.carlzhang.sinaapp.com/wx_carlzhang819.php)。在此过程中,需牢记在该php文件中定义的token值,此处示例为“weixin”,可在后续截图中标识出来。

5. 登录微信公众平台,进入开发者中心,开启“服务器配置”。在URL栏填入上一步复制的地址(注意:如果我的SAE默认版本是1,可能需要删除前面的1,因为SAE默认第一版。你可以尝试删除1,若访问不报404错误,则说明地址有效)。在Token栏填入代码中定义的token(上文提到的“weixin”)。


如果启用成功,关注你的微信平台并发送任意内容,检查是否收到提示“Welcome to wechat world!”或修改后的“Hello Word!”。
以上即为针对微信公众平台开发的完整讲解,希望能对大家有所帮助。