微信公众平台接口开发验证程序
? URL、TOKEN认证2种方式
成为开发者的第一步就是填写URL、TOKEN信息,来对你服务器进行验证
1) 把下面代码复制并保存为一个php文件(如weixin.php)
* wechat php test */
//define your token
define(\
$wechatObj = new wechatCallbackapiTest(); $wechatObj->valid();
class wechatCallbackapiTest { public function valid() {
$echoStr = $_GET[\
//valid signature , option if($this->checkSignature()){ echo $echoStr; exit; } }
public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS[\
//extract post data if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, LIBXML_NOCDATA);
'SimpleXMLElement', $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = \
}else { echo \ exit; } } private function checkSignature() {
$signature = $_GET[\ $timestamp = $_GET[\ $nonce = $_GET[\ $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{
return false; } } } ?>
2) 修改TOKEN
TOKEN是用来进行交互安全认证的,你自己随意定义,注意保证安全 定义后修改代码,在代码顶部找到 define(\
把值改为你自己的TOKEN值(如:mytoken),保存文件,然后上传到服务器,要确保可以访问
3) 填写URL TOKEN信息
回到公众平台页面,把URL TOKEN信息填写后提交,需要保证URL与上传的php文件地址一致,并且TOKEN值与php中定义的一致 信息填写正确后提交,正常会提示完成信息
至此,已经正式成为了开发者,接下来就可以安装自己的思路开发程序了
? 微信交互示例
做一个简单的示例:用户发送什么文字,我们就回复什么文字 注意:复制代码后要把TOKEN值修改为自己的
* wechat php test */
//define your token
define(\
$str = '';
$wechatObj = new wechatCallbackapiTest(); $wechatObj->valid();
class wechatCallbackapiTest { public function valid() {
$echoStr = $_GET[\
//valid signature , option if($this->checkSignature()){ echo $echoStr; $this->responseMsg(); exit; } }
public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS[\
//extract post data if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = \
}else {
echo \ exit; } } private function checkSignature() {
$signature = $_GET[\ $timestamp = $_GET[\ $nonce = $_GET[\ $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } } ?>
? 用户关注后发送欢迎信息
在实际应用中,用户第一次关注是,我们需要发送欢迎和提示信息,就需要下面的判断代码
if($keyword === \){ $contentStr = \欢迎信息\; }
开发文档中写明,用户关注后服务器自动发送过来的字符串为\,我们就依次来判断了
注意:信息模板中此项
此文档总结了微信公众平台开发的起步过程,接下来就需要大家发挥自己创造力了,祝大家开发出更多更有益的应用