最近有时间也没事做就想搞一下微信公众号开发,根据微信公众平台开发者文档一步一步来。以前做过ectouch商城二次开发,接触过微信公众号开发,所以前面配置问题不成问题。
当微信对接token验证失败的问题时,token验证总是失败。网上也查了不少的资料,还是不行。
再次检查这几项:
1、域名已经备案
2、检查编码问题,使用NotPad++打开token验证页面,转为UTF-8无BOM编码格式
3、检查输出问题,在返回echostr前面不能有任何的输出,即使是空格也不行。
如果这样还是不行的话,可以直接返回echostr,测试是否配置成功,如果可以成功,说明不是编码问题,是返回时前面有输出。
开发步骤:
1、引用类(使用的微信github,封装一个类)
2、填写公众号appid,appsecret,token
3、写代码,WechatController.class.php
<?php namespace Home\Controller; use OT\DataDictionary; class WechatController extends HomeController { private $weObj = ''; public function __construct(){ parent::__construct(); import('Vendor.wechat.wechat'); $configs['token'] =C('WX_TOKEN');//('WX_TOKEN');//['token']; $configs['appid'] =C('WX_APPID');// $wxinfo['appid']; $configs['appsecret'] =C('WX_APPSECRET');//$wxinfo['appsecret']; $this->weObj=new \Wechat($configs); } public function index(){ } //验证微信token_url public function wechat_token_url(){ $data=I('get.'); $resp=$this->weObj->valid($data); echo htmlspecialchars($resp); // $this->weObj->log2($resp,'log.log'); } }
代码整理