php微信卡劵教程,微信卡券添加功能的实现-PHP

一.后台实现

准备:①有效的appID ②有效的appSecret ③有效的cardID

以下为model类实现:

1)首先获取 access_token:

以get形式请求$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret";

(如果是企业号$url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$appId&corpsecret=$appSecret";)

[code]private function getAccessToken() {

// access_token 应该全局存储与更新,以下代码以写入到文件中做示例

$data = json_decode($this->get_php_file("access_token.php"));

if ($data->expire_time < time()) {

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

$res = json_decode($this->httpGet($url));

$access_token = $res->access_token;

if ($access_token) {

$data->expire_time = time() + 7000;

$data->access_token = $access_token;

$this->set_php_file("access_token.php", json_encode($data));

}

} else {

$access_token = $data->access_token;

}

return $access_token;

}

2)获取 jsapi_ticket:(请求次数有限制,建议全局存储与更新)

以get形式请求$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";

(如果是企业号$url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";)

[code]private function getJsApiTicket() {

// jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例

$data = json_decode($this->get_php_file("jsapi_ticket.php"));

if ($data->expire_time < time()) {

$accessToken = $this->getAccessToken();

$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";

$res = json_decode($this->httpGet($url));

$ticket = $res->ticket;

if ($ticket) {

$data->expire_time = time() + 7000;

$data->jsapi_ticket = $ticket;

$this->set_php_file("jsapi_ticket.php", json_encode($data));

}

} else {

$ticket = $data->jsapi_ticket;

}

return $ticket;

}

3)获取SignPackage:

将请求本接口的链接地址、时间戳、随机字符串及步骤2获得的jsapi_ticket,按照 key 值 ASCII 码升序排序,形如

[code]public function getSignPackage() {

$jsapiTicket = $this->getJsApiTicket();

$url = "http://xxxx";

$timestamp = time();

$nonceStr = $this->createNonceStr();

// 这里参数的顺序要按照 key 值 ASCII 码升序排序

$arrdata = array("timestamp" => $timestamp, "noncestr" => $nonceStr, "url" => $url, "jsapi_ticket" => $jsapiTicket);

ksort($arrdata);

$string = "";

foreach($arrdata as $key => $value){

if(strlen($string) == 0){

$string .= $key . "=" .$value;

}else{

$string .= "&" . $key . "=" .$value;

}

}

$signature = sha1($string);

$signPackage = array(

"jsapiTicket" => $jsapiTicket,

"appId" => $appId,

"nonceStr" => $nonceStr,

"timestamp" => $timestamp,

"url" => $url,

"signature" => $signature,

"rawString" => $string

);

return $signPackage;

}

4)获取 ApiTicket:(请求次数十分有限,建议全局存储与更新)

[code]private function getApiTicket(){

// ApiTicket 应该全局存储与更新,以下代码以写入到文件中做示例

$data = json_decode($this->get_php_file("api_ticket.php"));

if ($data->expire_time < time()){

$accessToken = $this->getAccessToken();

$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=wx_card&access_token=$accessToken";

$res = json_decode($this->httpGet($url));

$api_ticket = $res->ticket;

if ($api_ticket) {

$data->expire_time = time() + 7000;

$data->api_ticket = $api_ticket;

$this->set_php_file("api_ticket.php", json_encode($data));

}

} else {

$api_ticket = $data->api_ticket;

}

return $api_ticket;

}

5)获取 CardPackage:

[code]public function getCardPackage() {

$api_ticket = $this->getApiTicket();

$card_id = $cardId;

$nonceStr = $this->createNonceStr();

$url = "xxxx";

$timestamp = time();

$card =["$api_ticket",$timestamp,"$nonceStr","$card_id"];

sort($card,SORT_STRING);

$cardature = sha1(implode($card));

$cardPackage = array(

"api_tiket" => $api_ticket,

"timestamp" => $timestamp,

"url" => $url,

"card_id" => $card_id,

"cardature" => $cardature,

"nonceStr" => $nonceStr

);

return $cardPackage;

}

其他补充:

[code]//随机数

private function createNonceStr($length = 16) {

$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

$str = "";

for ($i = 0; $i < $length; $i++) {

$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);

}

return $str;

}

//get方法

private function httpGet($url) {

$curl = curl_init();

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl, CURLOPT_TIMEOUT, 500);

// 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。

// 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);

curl_setopt($curl,CURLOPT_CAINFO,dirname(__FILE__).'/cacert.pem');

curl_setopt($curl, CURLOPT_URL, $url);

$res = curl_exec($curl);

curl_close($curl);

return $res;

}

//存取文件

private function get_php_file($filename) {

return trim(substr(file_get_contents($filename), 15));

}

private function set_php_file($filename, $content) {

$fp = fopen($filename, "w");

fwrite($fp, "<?php exit();?>" . $content);

fclose($fp);

}

二.前端实现

准备:

①在得到上面这3个凭证之后就可以开始接下来的第二步:网站引入微信的JS文件,注入config配置.这一步操作需要注意的是,网站的域名必须在微信公众号后台添加到了"设置"->"公众号设置"->"功能设置"->"JS接口安全域名"里

②引入

1.H5页面中需要实现卡券的config配置:

[code]

$.ajax({

type: "POST",

url: "xxxx",

data: {},

timeout: 1e4,

cache:false,

dataType: "json",

async:false,

success: function(a) {

appid = a.signPackage.appId,

nonceStr = a.signPackage.nonceStr,

timestamp = a.signPackage.timestamp,

signature = a.signPackage.signature,

cardature = a.cardPackage.cardature,

tickt = a.cardPackage.api_tiket,

nonceStr2 = a.cardPackage.nonceStr,

card_id = a.cardPackage.card_id,

timestamp2 = a.cardPackage.timestamp,

wx.config({

appId: appid,

timestamp: timestamp,

nonceStr: nonceStr,

signature: signature,

jsApiList: ["addCard"]

})

},

error: function() {

alert("系统繁忙,请稍后再试!")

}

}),

2.配置成功的情况下写添加卡券方法:

[code]wx.ready(function(){

wx.addCard({

cardList: [

{

cardId: card_id,

cardExt: '{"timestamp": "' + timestamp2 + '","signature":"' + cardature + '","nonce_str": "' + nonceStr2 + '","card_id": "' + card_id + '"}'

}

],

success: function (res) {

alert('已添加卡券:' + JSON.stringify(res.cardList));

}

});

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值