100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 小程序统一服务消息接口

小程序统一服务消息接口

时间:2024-05-22 18:28:00

相关推荐

小程序统一服务消息接口

地址:/xinweiyun/p/9634147.html

下载地址:/download/weixin_38615720/10906803

昨天晚上,9月11日,微信官方又更新了一大波的小程序功能。重点我们来谈谈这个功能,微信叫做统一服务消息下发接口。

这个是官方的文档 统一服务消息 · 小程序

https://developers./miniprogram/dev/api/notice-uniform.html

统一服务消息下发接口,名字很拗口,说点人话,就是这个接口可以发送公众号或者小程序的消息。之前大家都知道,我们用小程序发送消息,有小程序的api接口,如果是发送公众号的消息,就有公众号的模板消息接口。

现在终于微信出来这个统一发送的了。这个接口一出来我们就尝试使用了下。有些问题大家需要留意下,不然到时候会遇到不少的坑。

access_token小程序的access_token 这个一定要用小程序的accesstoken,不能使用公众号的,因为这个接口的初衷就是想大家在开发小程序的时候,如果要发送公众号消息直接使用这个接口就可以了,无需再去调用公众号的模板消息接口。

touser用户openid,可以是小程序的openid,也可以是mp_template_msg.appid对应的公众号的openid 这个使用公众号的openid或者小程序的openid都可以的 微信官方会自动关联。其实大家的使用场景大部分还是在小程序,所以这里你就直接用小程序里抓到的openid就可以了。

mp_template_msg.appid公众号appid,要求与小程序有绑定且同主体 这个大家要仔细读官方的说明哦,就是这公众号和小程序要相互关联了,且必须是同主体才行。不然也会调试不通过的。

基本上只要掌握和留意了上面这几点,基本上就没问题了。如果还有问题,那就肯定是你传值不对了。你就要多检查了。最好自己打印出日志来。比如你传的值是什么,看看是否和文档里的演示demo一样。值的类型有没有传错之类的。

代码:

/**

* 抽奖结果 公众号模板消息

*/

public function integral_result(){

$access_token= $this->getWxAccessToken();

$url = "https://api./cgi-bin/message/wxopen/template/uniform_send?access_token=".$access_token;

$send_data = array();

$send_data['touser'] = "发送人openid";

$values = array();

//公众号appid

$values['appid'] = "";

//公众号模板id

$values['template_id'] = "";

//公众号模板消息所要跳转的url

$values['url'] = "/download";

//公众号模板消息所要跳转的小程序,小程序的必须与公众号具有绑定关系

$values['miniprogram'] = array(

"appid"=>"",

"path"=>"pages/index/index" //微信公众号发送模板的坑

);

$values['data'] = array(

"first"=>array(

"value"=>"抽奖结果通知",

"color"=>"#173177"

),

"keyword1"=>array(

"value"=>"最新的iphone一部,希望再接再励!!!",

"color"=>"#173177"

),

"keyword2"=>array(

"value"=>"恭喜你中奖啦",

"color"=>"#173177"

),

"remark"=>array(

"value"=>"点击查询详情",

"color"=>"#173177"

),

);

$send_data['mp_template_msg'] = $values;

$send_data_json = json_encode($send_data);

$res = $this->http_curl($url,"post",$send_data_json);

echo "<pre>";

print_r($res);

echo "</pre>";

exit();

}

/**

* 获取access_token

*/

public function getWxAccessToken(){

//缓存access_token

if($_SESSION['access_token'] && $_SESSION['access_token_expire_time']>time()){

$rs = $_SESSION['access_token'];

}else{

$appid = '小程序appid';

$secret = '小程序secret';

$url = "https://api./cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;

$res = $this->http_curl($url);

$rs = $res['access_token'];

$_SESSION['access_token'] = $rs;

$_SESSION['access_token_expire_time'] = time()+7000;

}

return $rs;

}

/**

* 封装curl

*/

public function http_curl($url, $type = 'get', $arr ='',$res ='json'){

$cl = curl_init();

curl_setopt($cl, CURLOPT_URL, $url);

curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);

if($type == 'post'){

curl_setopt($cl, CURLOPT_POST, 1);

curl_setopt($cl, CURLOPT_POSTFIELDS, $arr);

}

$output = curl_exec($cl);

curl_close($cl);

if($res == 'json'){

if( curl_error($cl)){

return curl_error($cl);

}else{

return json_decode($output, true);

}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。