现在做微信开发,验证开发者那里 , 按照接口文说的原样thinkphp 返回token了echostr参数还是token验证失败

PHP对接微信公众平台消息接口开发流程教程
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了PHP对接微信公众平台消息接口开发流程,如何使用PHP版接口操作公众平台消息,需要的朋友可以参考下
一、写好接口程序
在你的服务器上上传好一个接口程序文件,如/weixin.php& 内容如下:
代码如下:&?phpdefine("TOKEN", "weixin");//自己定义的token 就是个通信的私钥$wechatObj = new wechatCallbackapiTest();$wechatObj-&valid();//$wechatObj-&responseMsg();class wechatCallbackapiTest{&&& public function valid()&&& {&&&&&&& $echoStr = $_GET["echostr"];&&&&&&& if($this-&checkSignature()){&&&&&&&&&&& echo $echoS&&&&&&&&&&&&&&&&&& }&&& }&&& public function responseMsg()&&& {&&&&&&& $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];&&&&&&& if (!empty($postStr)){&&&&&&&&&&& $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);&&&&&&&&&&& $fromUsername = $postObj-&FromUserN&&&&&&&&&&& $toUsername = $postObj-&ToUserN&&&&&&&&&&& $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 = '你好啊,屌丝';&&&&&&&&&&&&&&& $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);&&&&&&&&&&&&&&& echo $resultS&&&&&&&&&&& }else{&&&&&&&&&&&&&&& echo '咋不说哈呢';&&&&&&&&&&& }&&&&&&& }else {&&&&&&&&&&& echo '咋不说哈呢';&&&&&&&&&&&&&&&&&& }&&& }&&& private function checkSignature()&&& {&&&&&&& $signature = $_GET["signature"];&&&&&&& $timestamp = $_GET["timestamp"];&&&&&&& $nonce = $_GET["nonce"];&&&&&&& $token =TOKEN;&&&&&&& $tmpArr = array($token, $timestamp, $nonce);&&&&&&& sort($tmpArr);&&&&&&& $tmpStr = implode( $tmpArr );&&&&&&& $tmpStr = sha1( $tmpStr );&&&&&&& if( $tmpStr == $signature ){&&&&&&&&&&&&&&&&&& }else{&&&&&&&&&&&&&&&&&& }&&& }}?&
二、配置微信公众平台回复接口
设置回复接口,填好URL和Token(url填上面的,token必须跟上面程序里面定义的Token一致)
三、验证接口
用自己的个人微信关注下你的公众账号,给这个账号发一条消息过去,收到原样的消息返回,即验证成功了。
四、开始自定义回复
注释掉$wechatObj-&valid(); 这行,同时去掉//$wechatObj-&responseMsg();这行的注释。你可以修改responseMsg函数里面的代码,根据用户的消息类型('text','image','location')和消息内容来回复用户不同的内容。消息接口就可以使用了,发个消息试试看吧?
1.封装weixin.class.php
由于微信公众平台的通信使用的是特定格式的XML数据,每次接受和回复都要去做一大堆的数据处理。我们就考虑在这个基础上做一次封装,weixin.class.php,代码如下: 代码如下:&?phpclass Weixin{&&& public $token = '';//token&&& public $debug =&//是否debug的状态标示,方便我们在调试的时候记录一些中间数据&&& public $setFlag =&&& public $msgtype = 'text';&& //('text','image','location')&&& public $msg = array();&&& public function __construct($token,$debug)&&& {&&&&&&& $this-&token = $&&&&&&& $this-&debug = $&&& }&&& //获得用户发过来的消息(消息内容和消息类型& )&&& public function getMsg()&&& {&&&&&&& $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];&&&&&&& if ($this-&debug) {&&&&&&&&&&&&&&&&&&&&&&& $this-&write_log($postStr);&&&&&&& }&&&&&&& if (!empty($postStr)) {&&&&&&&&&&& $this-&msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);&&&&&&&&&&& $this-&msgtype = strtolower($this-&msg['MsgType']);&&&&&&& }&&& }  //回复文本消息&&& public function makeText($text='')&&& {&&&&&&& $CreateTime = time();&&&&&&& $FuncFlag = $this-&setFlag ? 1 : 0;&&&&&&& $textTpl = "&xml&&&&&&&&&&&& &ToUserName&&![CDATA[{$this-&msg['FromUserName']}]]&&/ToUserName&&&&&&&&&&&& &FromUserName&&![CDATA[{$this-&msg['ToUserName']}]]&&/FromUserName&&&&&&&&&&&& &CreateTime&{$CreateTime}&/CreateTime&&&&&&&&&&&& &MsgType&&![CDATA&&&&&&&&&&& 1& &&&&&&&&&&& ]&&/MsgType&&&&&&&&&&&& &Content&&![CDATA[%s]]&&/Content&&&&&&&&&&&& &FuncFlag&%s&/FuncFlag&&&&&&&&&&&& &/xml&";&&&&&&& return sprintf($textTpl,$text,$FuncFlag);&&& }&&& //根据数组参数回复图文消息&&& public function makeNews($newsData=array())&&& {&&&&&&& $CreateTime = time();&&&&&&& $FuncFlag = $this-&setFlag ? 1 : 0;&&&&&&& $newTplHeader = "&xml&&&&&&&&&&&& &ToUserName&&![CDATA[{$this-&msg['FromUserName']}]]&&/ToUserName&&&&&&&&&&&& &FromUserName&&![CDATA[{$this-&msg['ToUserName']}]]&&/FromUserName&&&&&&&&&&&& &CreateTime&{$CreateTime}&/CreateTime&&&&&&&&&&&& &MsgType&&![CDATA[news]]&&/MsgType&&&&&&&&&&&& &Content&&![CDATA[%s]]&&/Content&&&&&&&&&&&& &ArticleCount&%s&/ArticleCount&&Articles&";&&&&&&& $newTplItem = "&item&&&&&&&&&&&& &Title&&![CDATA[%s]]&&/Title&&&&&&&&&&&& &Description&&![CDATA[%s]]&&/Description&&&&&&&&&&&& &PicUrl&&![CDATA[%s]]&&/PicUrl&&&&&&&&&&&& &Url&&![CDATA[%s]]&&/Url&&&&&&&&&&&& &/item&";&&&&&&& $newTplFoot = "&/Articles&&&&&&&&&&&& &FuncFlag&%s&/FuncFlag&&&&&&&&&&&& &/xml&";&&&&&&& $Content = '';&&&&&&& $itemsCount = count($newsData['items']);&&&&&&& $itemsCount = $itemsCount & 10 ? $itemsCount : 10;//微信公众平台图文回复的消息一次最多10条&&&&&&& if ($itemsCount) {&&&&&&&&&&& foreach ($newsData['items'] as $key =& $item) {&&&&&&&&&&&&&&& if ($key&=9) {&&&&&&&&&&&&&&&&&&& $Content .= sprintf($newTplItem,$item['title'],$item['description'],$item['picurl'],$item['url']);&&&&&&&&&&&&&&& }&&&&&&&&&&& }&&&&&&& }&&&&&&& $header = sprintf($newTplHeader,$newsData['content'],$itemsCount);&&&&&&& $footer = sprintf($newTplFoot,$FuncFlag);&&&&&&& return $header . $Content . $&&& }&&& public function reply($data)&&& {&&&&&&& if ($this-&debug) {&&&&&&&&&&&&&&&&&&& $this-&write_log($data);&&&&&&& }&&&&&&& echo $&&& }&&& public function valid()&&& {&&&&&&& if ($this-&checkSignature()) {&&&&&&&&&&& if( $_SERVER['REQUEST_METHOD']=='GET' )&&&&&&&&&&& {&&&&&&&&&&&&&&& echo $_GET['echostr'];&&&&&&&&&&&&&&&&&&&&&&&&&& }&&&&&&& }else{&&&&&&&&&&& write_log('认证失败');&&&&&&&&&&&&&&&&&& }&&& }&&& private function checkSignature()&&& {&&&&&&& $signature = $_GET["signature"];&&&&&&& $timestamp = $_GET["timestamp"];&&&&&&& $nonce = $_GET["nonce"];&&&&&&& $tmpArr = array($this-&token, $timestamp, $nonce);&&&&&&& sort($tmpArr);&&&&&&& $tmpStr = implode( $tmpArr );&&&&&&& $tmpStr = sha1( $tmpStr );&&&&&&& if( $tmpStr == $signature ){&&&&&&&&&&&&&&&&&& }else{&&&&&&&&&&&&&&&&&& }&&& }&&& private function write_log($log){&&&&&&& //这里是你记录调试信息的地方& 请自行完善&& 以便中间调试&&& }}?&
2.调用weixin.class.php
把你的微信公众平台主接口文件(如前面定义的/weixin.php)中,修改代码为: 代码如下:&?phpinclude_once('weixin.class.php');//引用刚定义的微信消息处理类define("TOKEN", "mmhelper");define('DEBUG', true);$weixin = new Weixin(TOKEN,DEBUG);//实例化$weixin-&getMsg();$type = $weixin-&//消息类型$username = $weixin-&msg['FromUserName'];//哪个用户给你发的消息,这个$username是微信加密之后的,但是每个用户都是一一对应的if ($type==='text') {&&& if ($weixin-&msg['Content']=='Hello2BizUser') {//微信用户第一次关注你的账号的时候,你的公众账号就会受到一条内容为'Hello2BizUser'的消息&&&&&&& $reply = $weixin-&makeText('欢迎你关注哦,屌丝');&&& }else{//这里就是用户输入了文本信息&&&&&&& $keyword = $weixin-&msg['Content'];&& //用户的文本消息内容&&&&&&&&&&&&&&& include_once("chaxun.php");//文本消息 调用查询程序 &&&&&&&&&&&&&&& $chaxun= new chaxun(DEBUG,$keyword,$username);&&&&&&&&&&&&&&& $results['items'] =$chaxun-&search();//查询的代码&&&&&&&&&&&&&&& $reply = $weixin-&makeNews($results);&&& }}elseif ($type==='location') {&&&&& //用户发送的是位置信息& 稍后的文章中会处理&&&&&&&&&&&&&&&& }elseif ($type==='image') {&&&&& //用户发送的是图片 稍后的文章中会处理}elseif ($type==='voice') {&&& &&&&& //用户发送的是声音 稍后的文章中会处理}$weixin-&reply($reply);?&
3.查询代码
还需要将数据库里面的查询结果格式化为特定的形式 代码如下:&?phppublic function search(){&&&&&& $record=array();& //定义返回结果的数组&&&&&& $list = $this-&search($this-&keyword);//普通的根据关键词查询数据库的操作& 代码就不用分享了    if(is_array($list)&&!empty($list)){&&&&&&&&&&&&&&& &&&&&&&&&&&&&& foreach($list as $msg){&&&         $record[]=array(//以下代码,将数据库中查询返回的数组格式化为微信返回消息能接收的数组形式,即title、description、picurl、url 详见微信官方的文档描述&&&&&&&&&&&&&&&&&&&&&&& 'title' =&$msg['title'],&&&&&&&&&&&&&&&&&&&&&&& 'description' =&$msg['discription'],&&&&&&&&&&&&&&&&&&&&&&& 'picurl' =& $msg['pic_url'],&&&&&&&&&&&&&&&&&&&&&&& 'url' =&$msg['url']&&&&&&&&&&&&&&&&&&& );        }    }    return $}?&
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具使用微信公众平台开发发者模式,第一件事就是要通过验证,就是token的验证。很多用户在开发微信公众平台的时候,对于第一次公众平台身份验证(Token 认证)走了不少弯路,现将微信公众平台开发验证签名的代码分享:
private static string Token = &微时代&;//此次填写您的Token 在微信公众平台里面
必须开启开发模式,然后点击进入
#region 验证
&&&&&&& public void valid()
&&&&&&&&&&& var echostr = Request[&echoStr&];
&&&&&&&&&&& if (checkSignature() && !string.IsNullOrEmpty(echostr))
&&&&&&&&&&& {
&&&&&&&&&&&&&&& Response.Write(echostr);//推送
&&&&&&&&&&&&&&& Response.End();&&&&&&&&&&
&&&&&&& public bool checkSignature()
&&&&&&&&&&& var signature = Request[&signature&];
&&&&&&&&&&& var timestamp = Request[&timestamp&];
&&&&&&&&&&& var nonce = Request[&nonce&];
&&&&&&&&&&& var token = T
&&&&&&&&&&& string[] ArrTmp = { token, timestamp, nonce };
&&&&&&&&&&& Array.Sort(ArrTmp);&&&& //字典排序
&&&&&&&&&&& string tmpStr = string.Join(&&, ArrTmp);
&&&&&&&&&&& tmpStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, &SHA1&);
&&&&&&&&&&& tmpStr = tmpStr.ToLower();
&&&&&&&&&&& if (tmpStr == signature)
&&&&&&&&&&& {
&&&&&&&&&&&&&&&
&&&&&&&&&&& }
&&&&&&&&&&& else
&&&&&&&&&&& {
&&&&&&&&&&&&&&&
&&&&&&&&&&& }
&&&&&&& public string GetSha1(System.Collections.Generic.List&string& codelist)
&&&&&&&&&&& codelist.Sort();
&&&&&&&&&&& var combostr = string.E
&&&&&&&&&&& for (int i = 0; i & codelist.C i++)
&&&&&&&&&&& {
&&&&&&&&&&&&&&& combostr += codelist[i];
&&&&&&&&&&& }
&&&&&&&&&&& return EncryptToSHA1(combostr);
&&&&&&& public string EncryptToSHA1(string str)
&&&&&&&&&&& System.Security.Cryptography.SHA1CryptoServiceProvider sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
&&&&&&&&&&& byte[] str1 = System.Text.Encoding.UTF8.GetBytes(str);
&&&&&&&&&&& byte[] str2 = puteHash(str1);
&&&&&&&&&&& sha1.Clear();
&&&&&&&&&&& (sha1 as IDisposable).Dispose();
&&&&&&&&&&& return Convert.ToBase64String(str2);
&&&&&&& #endregion
调用示例:
if (!string.IsNullOrEmpty(equest[&echoStr&]))
&&&&&&&&&&&&&&& {
&&&&&&&&&&&&&&&&&& valid();//用于验证
&&&&&&&&&&&&&&& }
以上代码完整无误,可以直接复制过去使用!进行了上面的认证之后您就可以使用微信公众平台的接口了!
上一篇:下一篇:首先到开发者中心启用服务器配置,填写好URL和Token,后面就是写代码了。
在我们首次提交验证申请时,微信服务器将发送GET请求到填写的URL上,并且带上四个参数(signature、timestamp、nonce、echostr),通过对签名(即signature)的效验,来判断此条消息的真实性。此后,每次接收用户消息的时候,微信也都会带上这三个参数(signature、timestamp、nonce)访问我们设置的URL,和第一次相同我们依然需要通过对签名的效验判断此条消息的真实性。效验方式与首次提交验证申请一致。
微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
随机字符串
通过检验signature对请求进行校验(代码在下面提供)。若确认此次GET请求来自微信服务器,则原样返回echostr参数内容,则接入生效,成为开发者成功,否则接入失败。
加密/校验流程如下:1. 将token、timestamp、nonce三个参数进行字典序排序2. 将三个参数字符串拼接成一个字符串进行sha1加密3. 将加密后的字符串与signature对比,标识该请求来源于微信
详细代码如下:
function checkSignature($token) {
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
return sha1($tmpStr) == $
// 微信公众后台填写的Token
$token = 'theToken';
// 如果验证正确,则返回参数echostr的内容,否则终止执行
if(checkSignature($token)) {
echo $_GET['echostr'];
OK!到这里差不多结束了,快去微信后台看看能否验证成功!
最后,如果有问题,可以给我留言&&
阅读(...) 评论()

我要回帖

更多关于 微信开发者平台 token 的文章

 

随机推荐