微信开发者验证方法及程序

2019-01-10 10:25

微信公众平台接口开发验证程序

? 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 = \ %s 0 \ if(!empty( $keyword )) { $msgType = \ $contentStr = \ $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo \ }

}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 = \ %s 1 \ if(!empty( $keyword )) { $msgType = \ $contentStr = $keyword; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo \ }

}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 = \欢迎信息\; }

开发文档中写明,用户关注后服务器自动发送过来的字符串为\,我们就依次来判断了

注意:信息模板中此项1的值要设为1,这样用户微信中才会显示有新消息的标志

此文档总结了微信公众平台开发的起步过程,接下来就需要大家发挥自己创造力了,祝大家开发出更多更有益的应用


微信开发者验证方法及程序.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2017年部编新人教版八年级上册古诗文理解性默写(完整版)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: