为啥,模板消息执行后,显示access_token无效?

来源:1-1 模板消息接口介绍

chanson1216

2017-04-20 16:32

一直显示40001错误,

function sendTemplateMsg()
	{
		//echo '112';
		// 1.获取到access_token
		$access_token = $this->getWxAccessToken();
		$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
		// 2.组装数组
/*		 {
           "touser":"OPENID",
           "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
           "url":"http://weixin.qq.com/download",  
           "miniprogram":{
             "appid":"xiaochengxuappid12345",
             "pagepath":"index?foo=bar"
           },          
           "data":{
                   "first": {
                       "value":"恭喜你购买成功!",
                       "color":"#173177"
                   },
           }
       }*/
		$arrayres = array(
				'touser'=>'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%',
				'template_id'=>'qaTs4ocx9A15_tQLtagY48oP6OW6DpDJh6e5g64_McE',//模板ID(用于接口调用)
				'url'=>'http://www.baidu.cn/',
				'miniprogram'=>array(
							'name'=>array('value'=>'hello zsong','color'=>"#173177"),
							'money'=>array('value'=>120,'color'=>"#173177"),
							'date'=>array('value'=>date('Y-m-d H:i:s'),'color'=>"#173177"),
						),//miniprogram array
					);
		// 3.将数组转化成json
		$postJson = json_encode($arrayres);
		// 4.调用curl函数
		$res = $this->http_curl($url,'post','json',$postJson);
		var_dump($res);
	}//sendTemplateMsg end
function getWxAccessToken()
	{
		// 由于access_token有过期时间,所以将access_token存放到session/cookie中。
		if ($_SESSION['access_token'] && $_SESSION['expire_time']>time()) {
			//如果access_token没有过期
			return $_SESSION['access_token'];
		} else {
			//如果access_token已经过期,需要重新获取access_token
			$appid 		= '%%%%%%%%%%%%%%%%%%%%%%%';//测试账号的Id和secret:wx5104563ab1ceb261
		 	$appsecret	= '%%%%%%%%%%%%%%%%%%%%%%%%%%%';
			$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
			$res = $this->http_curl($url,'get','json');
			$access_token = $res['access_token'];
			// 将获取之后的access_token存放到session
			$_SESSION['access_token'] = $access_token;
			$_SESSION['expire_time']  = time()+7000;

			return $access_token;
		}
	}


写回答 关注

2回答

  • 慕粉4269399
    2018-10-18 12:06:48

    楼上的问题解决了吗  求大神解答

  • chanson1216
    2017-04-20 16:33:18
    array(2) { ["errcode"]=> int(40001) ["errmsg"]=> string(81) "invalid 
    credential, access_token is invalid or not latest hint: [kHNwQA0894vr43!]" }


PHP微信公众平台开发高级篇—模板消息接口

模板消息,快到碗里来,可以应用于刷卡通知,商品购买成功通知

21658 学习 · 37 问题

查看课程

相似问题