1、首先获取access_token
access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。正常情况下access_token有效期为7200秒,重复获取将导致上次获取的access_token失效。
公众号可以使用AppID和AppSecret调用本接口来获取access_token。AppID和AppSecret可在开发模式中获得(需要已经成为开发者,且帐号没有异常状态)。注意调用所有微信接口时均需使用https协议。
接口调用请求说明
http请求方式: GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
参数说明
参数 | 是否必须 | 说明 |
---|---|---|
grant_type | 是 | 获取access_token填写client_credential |
appid | 是 | 第三方用户唯一凭证 |
secret | 是 | 第三方用户唯一凭证密钥,既appsecret |
返回说明
正常情况下,微信会返回下述JSON数据包给公众号:
{"access_token":"ACCESS_TOKEN","expires_in":7200}
参数 | 说明 |
---|---|
access_token | 获取到的凭证 |
expires_in | 凭证有效时间,单位:秒 |
错误时微信会返回错误码等信息,JSON数据包示例如下(该示例为AppID无效错误):
{"errcode":40013,"errmsg":"invalid appid"}
根据AppId和AppSecret,以https get方式获取访问特殊接口所必须的凭证access_token;
根据access_token,将json格式的菜单数据通过https post方式提交。
接入公众号
private $token;
public function index($id=''){
$mp = M('mp')->find($id);
$this->token = $mp['access_token'];
include_once APP_PATH . 'LaneWeChat/lanewechat.php';
//初始化微信类
$wechat = new WeChat($this->token, TRUE);
$wechat->checkSignature();
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_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 = "<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()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = $this->token;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
把数据存入数据库先把数据转换成数组形式
$mp=$this->mp;
$data=M('mp_menu')->where("mp_id={$mp['id']}")->order('id')->select();
$data2=$data;
foreach ($data as $k => $v) {
foreach ($data2 as $k2 => $v2) {
if ($v['index']==$v2['pindex']) {
$data[$k]['sub'][]=$v2;
unset($data[$k2]);
}
}
}
把数据添加如数据库这里引用了LaneWeChat框架
$ta=M('mp_menu');
$ta->where("mp_id={$mp['id']}")->delete();
$ta->addAll($data1);
include_once APP_PATH . 'LaneWeChat/lanewechat.php';
$ret= Menu::setMenu($data1);
// file_put_contents('menu.txt', $ret);
if ($ret === true) {
$this->ajaxReturn(array('msg'=>'添加成功'));
}else{
$this->ajaxReturn(array('msg'=>$ret));
}
数据的同步
$mp_id = $mp['id'];
include APP_PATH . 'LaneWeChat/lanewechat.php';
$menu = Menu::getMenu($data1);
// print_r($menu);
// // exit;
// print_r($menu['menu']['button']);
// exit;
$menu = $menu['menu']['button'];
$arr = array();
$index = 1;
foreach ($menu as $key => &$value) {
$value['mp_id']= $mp_id;
$value['index'] = $index;
$value['pindex'] = 0;
$value['sort'] = $index;
if(!empty($value['sub_button'])){
$value['type'] = '';//设置一级菜单,默认值
$value['content'] = '';//设置一级菜单,默认值
$sub_menu = $value['sub_button']; //把二级菜单入到变量$sub_menu
unset($value['sub_button']);
$arr[] = $value;
$subindex = 1;
foreach ($sub_menu as $subkey=>&$subvalue) {
$subvalue['mp_id'] = $mp_id;
$subvalue['index'] = $index . $subindex;
$subvalue['pindex'] = $index;
$subvalue['sort'] = $subindex;
if($subvalue['type']== 'click'){
$subvalue['content'] = $subvalue['key'];
unset($subvalue['key']);
}else if($subvalue['type']=='view'){
$subvalue['content'] = $subvalue['url'];
unset($subvalue['url']);
}else {
$subvalue['content'] = $subvalue['type'];
$subvalue['type'] = 'event';
unset($subvalue['key']);
}
unset($subvalue['sub_button']);
$arr[] = $subvalue;
$subindex++;
}
}else{
if($value['type']== 'click'){
$value['content'] = $value['key'];
unset($value['key']);
}else if($value['type']=='view'){
$value['content'] = $value['url'];
unset($value['url']);
}else {
$value['content'] = $value['type'];
$value['type'] = 'event';
unset($value['key']);
}
unset($value['sub_button']);
$arr[] = $value;
}
$index++;