php微信公众号卡券领取到卡包
第一步获取 token;
第二步获取 jsapi_ticket:
//获取jsapi_ticket
$jsapi_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$ACCESS_TOKEN&type=jsapi";
$getdata = do_post_request($jsapi_url);
$arr_jsapi = json_decode($getdata, true);
$jsapi_ticket = $arr_jsapi['ticket'];
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#10
第三步获取 api_ticket:
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#53
//获取卡券api_ticket
$api_ticket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$ACCESS_TOKEN&type=wx_card";
$getdata2 = do_post_request($api_ticket);
$arr_api = json_decode($getdata2, true);
$api_tickets = $arr_api['ticket'];
//生成随机字符串
function generateNonceStr($length = 16)
{
// 密码字符集,可任意添加你需要的字符
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= $chars[mt_rand(0, strlen($chars) - 1)];
}
return $str;
}
$n = generateNonceStr();
// ??
//获取使用接口的url地址要动态获取
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url1 = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
//生成js的签名函数包括参数jsapi_ticket,noncestr,timestamp,url,
//参与签名的字段包括noncestr(随机字符串), 有效的jsapi_ticket, timestamp(时间戳)
//, url(当前网页的URL,不包含#及其后面部分) 。
//对所有待签名参数按照字段名的ASCII 码从小到大排序(字典序)后,
//使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串string1。
//这里需要注意的是所有参数名均为小写字符。对string1作sha1加密,字段名和字段值都采用原始值,
//不进行URL 转义。
$str = 'jsapi_ticket=' . $jsapi_ticket . '&noncestr=' . $n . '×tamp=' . $time . '&url=' . $url1;
//将$str进行sha1加密就得到了js的签名
$jsSign = sha1($str);
//生成卡券的签名(参与签名的参数一定要和cardList里面的参数一样否则会报错),卡券的签名和js的签名是不一样的
//签名算法是按照这几个参数值按照字典顺序排列之后再按照sha1加密
$zs_str_yhq = "select id_card,num from yhq where id_sh='A000100062' and num>0";
$zs_str_arr = mysql_query($zs_str_yhq);
while ($zs_yhq_arr = mysql_fetch_array($zs_str_arr)){
for($i=$zs_yhq_arr["num"];$i>0;$i--)
{
$arr = array($zs_yhq_arr[id_card], $api_tickets, $time); //组装参数
asort($arr, SORT_STRING);
$sortString = "";
foreach ($arr as $temp) {
$sortString = $sortString . $temp;
}
$cardSign = sha1($sortString);
/*
*组合卡券列表 领取多个卡券到卡包
*/
$card_kb[] = [
'cardId'=>$zs_yhq_arr['id_card'],
'cardExt'=>[
"timestamp"=>$time,"signature"=>$cardSign
]
];
}
}
$cardlist = json_encode($card_kb);
多个优惠券领取到卡包
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button onclick="nonee()">领取卡券</button>
</body>
<script src="../js/jquery-3.2.1.min.js"></script>
<script src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
<script>
function nonee() {
wx.config({
debug: false,
appId: "<?php echo $appid?>",
timestamp:"<?php echo $time?>",
nonceStr: "<?php echo $n?>",
signature: "<?php echo $jsSign?>",
jsApiList: [
'addCard'
]
});
wx.ready(function() {
//添加卡券 批量优惠券
wx.addCard({
cardList:<?php echo $cardlist?>,
success: function(res) {
//alert('已添加卡券:' + JSON.stringify(res.cardList));
}
});
});
}
/**
*领取多个 (卡券签名错误)
* wx.ready(function() {
//添加卡券
wx.addCard({
cardList: [
{
cardId:"p8iCGwfaXGGZAxxTqLRoNX1Od8Ks",//pqD_dt-MGJTYkIaGOFJ6NiHM0_Ow["p8iCGwfaXGGZAxxTqLRoNX1Od8Ks","p8iCGwcpvbbnG6ESIJq4QBSMWBog"]
cardExt: '{"timestamp":"<?php echo $time?>","signature":"<?php echo $cardSign?>"}',
},
{
cardId:"p8iCGwcpvbbnG6ESIJq4QBSMWBog",//pqD_dt-MGJTYkIaGOFJ6NiHM0_Ow["p8iCGwfaXGGZAxxTqLRoNX1Od8Ks","p8iCGwcpvbbnG6ESIJq4QBSMWBog"]
cardExt: '{"timestamp":"<?php echo $time?>","signature":"<?php echo $cardSign?>"}',
}
],
success: function(res) {
//alert('已添加卡券:' + JSON.stringify(res.cardList));
}
});
});
*/
</script>
</html>
单个优惠券领取到卡包
<?php
//post函数
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url");
}
return $response;
}
//获取access_token
$TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$json=file_get_contents($TOKEN_URL);
$result=json_decode($json,true);
$ACCESS_TOKEN=$result['access_token'];
$time = time();
//获取jsapi_ticket
$jsapi_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$ACCESS_TOKEN&type=jsapi";
$getdata = do_post_request($jsapi_url);
$arr_jsapi = json_decode($getdata, true);
$jsapi_ticket = $arr_jsapi['ticket'];
//获取卡券api_ticket
$api_ticket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$ACCESS_TOKEN&type=wx_card";
$getdata2 = do_post_request($api_ticket);
$arr_api = json_decode($getdata2, true);
$api_tickets = $arr_api['ticket'];
//生成随机字符串
function generateNonceStr($length = 16)
{
// 密码字符集,可任意添加你需要的字符
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= $chars[mt_rand(0, strlen($chars) - 1)];
}
return $str;
}
$n = generateNonceStr();
// ??
//获取使用接口的url地址要动态获取
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url1 = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
//生成js的签名函数包括参数jsapi_ticket,noncestr,timestamp,url,
//参与签名的字段包括noncestr(随机字符串), 有效的jsapi_ticket, timestamp(时间戳)
//, url(当前网页的URL,不包含#及其后面部分) 。
//对所有待签名参数按照字段名的ASCII 码从小到大排序(字典序)后,
//使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串string1。
//这里需要注意的是所有参数名均为小写字符。对string1作sha1加密,字段名和字段值都采用原始值,
//不进行URL 转义。
$str = 'jsapi_ticket=' . $jsapi_ticket . '&noncestr=' . $n . '×tamp=' . $time . '&url=' . $url1;
//将$str进行sha1加密就得到了js的签名
$jsSign = sha1($str);
//生成卡券的签名(参与签名的参数一定要和cardList里面的参数一样否则会报错),卡券的签名和js的签名是不一样的
//签名算法是按照这几个参数值按照字典顺序排列之后再按照sha1加密
// ??
//获取使用接口的url地址要动态获取
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url1 = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$str = 'jsapi_ticket=' . $jsapi_ticket . '&noncestr=' . $n . '×tamp=' . $time . '&url=' . $url1;
$jsSign = sha1($str);
//签名算法是按照这几个参数值按照字典顺序排列之后再按照sha1加密
$arr = array("p8iCGwfaXGGZAxxTqLRoNX1Od8Ks", $api_tickets, $time); //组装参数
asort($arr, SORT_STRING);
$sortString = "";
foreach ($arr as $temp) {
$sortString = $sortString . $temp;
}
$cardSign = sha1($sortString);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button onclick="nonee()">领取卡券</button>
</body>
<script src="../js/jquery-3.2.1.min.js"></script>
<script src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
<script>
function nonee() {
wx.config({
debug: false,
appId: "<?php echo $appid?>",
timestamp:"<?php echo $time?>",
nonceStr: "<?php echo $n?>",
signature: "<?php echo $jsSign?>",
jsApiList: [
'addCard'
]
});
wx.ready(function() {
//添加卡券
wx.addCard({
cardList: [{
cardId: "p8iCGwfaXGGZAxxTqLRoNX1Od8Ks",
cardExt: '{"timestamp":"<?php echo $time ?>","signature":"<?php echo $cardSign ?>"}'
}],
success: function(res) {
//alert('已添加卡券:' + JSON.stringify(res.cardList));
}
});
});
}
</script>
</html>