这篇文章介绍的内容是关于微信api接口 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
本文给大家介绍的是个人总结的一些微信api接口,包括微信支付、微信红包、微信卡券、微信小店等,十分的全面,有需要的小伙伴可以参考下。
微信入口绑定,微信事件处理,微信api全部操作包含在这些文件中。
微信支付、微信红包、微信卡券、微信小店。
1. [代码]index.php
<?php
include_once 'lib.inc.php';
$wcobj = new wechat("youkuiyuan");
$wcobj->wcvalid();
2. [代码]微信入口类
<?php
/**
* description of wechat
*
* @author administrator
*/
class wechat extends wxapi{
public $token = "";
//put your code here
public function __construct($token = "") {
parent::__construct();
$this->token = $token;
}
public function wcchecksignature(){
try{
if (empty($this->token)) {
throw new exception('token is not defined!');
}
$signature = $_get["signature"];
$timestamp = $_get["timestamp"];
$nonce = $_get["nonce"];
$token = $this->token;
$tmparr = array($token, $timestamp, $nonce);
// use sort_string rule
sort($tmparr, sort_string);
$tmpstr = implode( $tmparr );
$tmpstr = sha1( $tmpstr );
if( $tmpstr == $signature ){
return true;
}else{
return false;
}
}
catch (exception $e) {
echo 'message: ' .$e->getmessage();
}
}
public function wcvalid(){
$echostr = isset($_get["echostr"]) && !empty($_get["echostr"]) ? addslashes($_get["echostr"]) : null;
if(is_null($echostr)){
$this->wcmsg();
}
else{
//valid signature , option
if($this->wcchecksignature()){
echo $echostr;
exit;
}
else{
exit();
}
}
}
public function wcmsg(){
//get post data, may be due to the different environments
$poststr = isset($globals["http_raw_post_data"]) && !empty($globals["http_raw_post_data"]) ? $globals["http_raw_post_data"] : "";
if(!empty($poststr)){
libxml_disable_entity_loader(true);
$postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);
$this->zclog(true,$postobj);
$fromusername = $postobj->fromusername;
$tousername = $postobj->tousername;
$msgtype = $postobj->msgtype;
if($msgtype == 'event'){//执行事件相应
$event = $postobj->event;
switch ($event) {
case 'subscribe'://关注
break;
case 'unsubscribe'://取消关注
break;
case 'scan'://扫描
break;
case 'location'://地址
break;
case 'click'://点击时间
break;
case 'view'://跳转
break;
case 'card_pass_check'://卡券审核通过
break;
case 'card_not_pass_check'://卡券审核失败
break;
case 'user_get_card'://用户领取卡券
break;
case 'user_del_card'://用户删除卡券
break;
case 'user_view_card'://用户浏览会员卡
break;
case 'user_consume_card'://用户核销卡券
break;
case 'merchant_order'://微小店用户下单付款
break;
default:
break;
}
}
else{
switch ($msgtype) {
case 'text'://文本格式
break;
case 'image'://图片格式
break;
case 'voice'://声音
break;
case 'video'://视频
break;
case 'shortvideo'://小视频
break;
case 'location'://上传地理位置
break;
case 'link'://链接相应
break;
default:
break;
}
}
////////////////////////////////////////////////////////////////////
$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;
}
}
//日志log
public function zclog($errcode , $errmsg){
$this->returnay = array();
$this->returnay['errcode'] = $errcode;
$this->returnay['errmsg'] = $errmsg;
$this->returnay['errtime'] = date("y-m-d h:i:s",time());
$logfile = fopen("logfile_".date("ymd",time()).".txt", "a+");
$txt = json_encode($this->returnay)."\n";
fwrite($logfile, $txt);
fclose($logfile);
//return $this->returnay;
}
}
3. [代码]微信操作类 - 更新了自定义菜单部分
<?php
/********************************************************
* @author kyler you <qq:2444756311>
* @link http://mp.weixin.qq.com/wiki/home/index.html
* @version 2.0.1
* @uses $wxapi = new wxapi();
* @package 微信api接口 陆续会继续进行更新
********************************************************/
class wxapi {
//const appid = "";
//const appsecret = "";
const appid = "";
const appsecret = "";
//const mchid = ""; //商户号
//const privatekey = ""; //私钥
public $parameters = array();
public function __construct(){
}
/****************************************************
* 微信提交api方法,返回微信指定json
****************************************************/
public function wxhttpsrequest($url,$data = null){
$curl = curl_init();
curl_setopt($curl, curlopt_url, $url);
curl_setopt($curl, curlopt_ssl_verifypeer, false);
curl_setopt($curl, curlopt_ssl_verifyhost, false);
if (!empty($data)){
curl_setopt($curl, curlopt_post, 1);
curl_setopt($curl, curlopt_postfields, $data);
}
curl_setopt($curl, curlopt_returntransfer, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
/****************************************************
* 微信带证书提交数据 - 微信红包使用
****************************************************/
public function wxhttpsrequestpem($url, $vars, $second=30,$aheader=array()){
$ch = curl_init();
//超时时间
curl_setopt($ch,curlopt_timeout,$second);
curl_setopt($ch,curlopt_returntransfer, 1);
//这里设置代理,如果有的话
//curl_setopt($ch,curlopt_proxy, '10.206.30.98');
//curl_setopt($ch,curlopt_proxyport, 8080);
curl_setopt($ch,curlopt_url,$url);
curl_setopt($ch,curlopt_ssl_verifypeer,false);
curl_setopt($ch,curlopt_ssl_verifyhost,false);
//以下两种方式需选择一种
//第一种方法,cert 与 key 分别属于两个.pem文件
//默认格式为pem,可以注释
curl_setopt($ch,curlopt_sslcerttype,'pem');
curl_setopt($ch,curlopt_sslcert,getcwd().'/apiclient_cert.pem');
//默认格式为pem,可以注释
curl_setopt($ch,curlopt_sslkeytype,'pem');
curl_setopt($ch,curlopt_sslkey,getcwd().'/apiclient_key.pem');
curl_setopt($ch,curlopt_cainfo,'pem');
curl_setopt($ch,curlopt_cainfo,getcwd().'/rootca.pem');
//第二种方式,两个文件合成一个.pem文件
//curl_setopt($ch,curlopt_sslcert,getcwd().'/all.pem');
if( count($aheader) >= 1 ){
curl_setopt($ch, curlopt_httpheader, $aheader);
}
curl_setopt($ch,curlopt_post, 1);
curl_setopt($ch,curlopt_postfields,$vars);
$data = curl_exec($ch);
if($data){
curl_close($ch);
return $data;
}
else {
$error = curl_errno($ch);
echo "call faild, errorcode:$error\n";
curl_close($ch);
return false;
}
}
/****************************************************
* 微信获取accesstoken 返回指定微信公众号的at信息
****************************************************/
public function wxaccesstoken($appid = null , $appsecret = null){
$appid = is_null($appid) ? self::appid : $appid;
$appsecret = is_null($appsecret) ? self::appsecret : $appsecret;
$data = json_decode(file_get_contents("access_token.json"));
if ($data->expire_time < time()) {
//echo $appid,$appsecret;
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
$result = $this->wxhttpsrequest($url);
//print_r($result);
$jsoninfo = json_decode($result, true);
$access_token = $jsoninfo["access_token"];
if ($access_token) {
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
$fp = fopen("access_token.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
}
else {
$access_token = $data->access_token;
}
return $access_token;
}
/****************************************************
* 微信获取accesstoken 返回指定微信公众号的at信息
****************************************************/
public function wxjsapiticket($appid = null , $appsecret = null){
$appid = is_null($appid) ? self::appid : $appid;
$appsecret = is_null($appsecret) ? self::appsecret : $appsecret;
$data = json_decode(file_get_contents("jsapi_ticket.json"));
if ($data->expire_time < time()) {
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=".$this->wxaccesstoken();
$result = $this->wxhttpsrequest($url);
$jsoninfo = json_decode($result, true);
$ticket = $jsoninfo['ticket'];
if ($ticket) {
$data->expire_time = time() + 7000;
$data->jsapi_ticket = $ticket;
$fp = fopen("jsapi_ticket.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
}
else {
$ticket = $data->jsapi_ticket;
}
return $ticket;
}
/****************************************************
* 微信通过openid获取用户信息,返回数组
****************************************************/
public function wxgetuser($openid){
$wxaccesstoken = $this->wxaccesstoken();
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$wxaccesstoken."&openid=".$openid."&lang=zh_cn";
$result = $this->wxhttpsrequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
}
/****************************************************
* 微信生成二维码ticket
****************************************************/
public function wxqrcodeticket($jsondata){
$wxaccesstoken = $this->wxaccesstoken();
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$wxaccesstoken;
$result = $this->wxhttpsrequest($url,$jsondata);
return $result;
}
/****************************************************
* 微信通过ticket生成二维码
****************************************************/
public function wxqrcode($ticket){
$url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . urlencode($ticket);
return $url;
}
/****************************************************
* 发送自定义的模板消息
****************************************************/
public function wxsetsend($touser, $template_id, $url, $data, $topcolor = '#7b68ee'){
$template = array(
'touser' => $touser,
'template_id' => $template_id,
'url' => $url,
'topcolor' => $topcolor,
'data' => $data
);
$jsondata = json_encode($template);
$result = $this->wxsendtemplate($jsondata);
return $result;
}
/****************************************************
* 微信设置oauth跳转url,返回字符串信息 - scope = snsapi_base //验证时不返回确认页面,只能获取openid
****************************************************/
public function wxoauthbase($redirecturl,$state = "",$appid = null){
$appid = is_null($appid) ? self::appid : $appid;
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirecturl."&response_type=code&scope=snsapi_base&state=".$state."#wechat_redirect";
return $url;
}
/****************************************************
* 微信设置oauth跳转url,返回字符串信息 - scope = snsapi_userinfo //获取用户完整信息
****************************************************/
public function wxoauthuserinfo($redirecturl,$state = "",$appid = null){
$appid = is_null($appid) ? self::appid : $appid;
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirecturl."&response_type=code&scope=snsapi_userinfo&state=".$state."#wechat_redirect";
return $url;
}
/****************************************************
* 微信oauth跳转指定url
****************************************************/
public function wxheader($url){
header("location:".$url);
}
/****************************************************
* 微信通过oauth返回页面中获取at信息
****************************************************/
public function wxoauthaccesstoken($code,$appid = null , $appsecret = null){
$appid = is_null($appid) ? self::appid : $appid;
$appsecret = is_null($appsecret) ? self::appsecret : $appsecret;
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
$result = $this->wxhttpsrequest($url);
//print_r($result);
$jsoninfo = json_decode($result, true);
//$access_token = $jsoninfo["access_token"];
return $jsoninfo;
}
/****************************************************
* 微信通过oauth的access_token的信息获取当前用户信息 // 只执行在snsapi_userinfo模式运行
****************************************************/
public function wxoauthuser($oauthat,$openid){
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$oauthat."&openid=".$openid."&lang=zh_cn";
$result = $this->wxhttpsrequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
}
/****************************************************
* 创建自定义菜单
****************************************************/
public function wxmenucreate($jsondata){
$wxaccesstoken = $this->wxaccesstoken();
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $wxaccesstoken;
$result = $this->wxhttpsrequest($url,$jsondata);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
}
/****************************************************
* 获取自定义菜单
****************************************************/
public function wxmenuget(){
$wxaccesstoken = $this->wxaccesstoken();
$url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" . $wxaccesstoken;
$result = $this->wxhttpsrequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
}
/****************************************************
* 删除自定义菜单
****************************************************/
public function wxmenudelete(){
$wxaccesstoken = $this->wxaccesstoken();
$url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" . $wxaccesstoken;
$result = $this->wxhttpsrequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
}
/****************************************************
* 获取第三方自定义菜单
****************************************************/
public function wxmenugetinfo(){
$wxaccesstoken = $this->wxaccesstoken();
$url = "https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token=" . $wxaccesstoken;
$result = $this->wxhttpsrequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
}
/*****************************************************
* 生成随机字符串 - 最长为32位字符串
*****************************************************/
public function wxnoncestr($length = 16, $type = false) {
$chars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
if($type == true){
return strtoupper(md5(time() . $str));
}
else {
return $str;
}
}
/*******************************************************
* 微信商户订单号 - 最长28位字符串
*******************************************************/
public function wxmchbillno($mchid = null) {
if(is_null($mchid)){
if(self::mchid == "" || is_null(self::mchid)){
$mchid = time();
}
else{
$mchid = self::mchid;
}
}
else{
$mchid = substr(addslashes($mchid),0,10);
}
return date("ymd",time()).time().$mchid;
}
/*******************************************************
* 微信格式化数组变成参数格式 - 支持url加密
*******************************************************/
public function wxsetparam($parameters){
if(is_array($parameters) && !empty($parameters)){
$this->parameters = $parameters;
return $this->parameters;
}
else{
return array();
}
}
/*******************************************************
* 微信格式化数组变成参数格式 - 支持url加密
*******************************************************/
public function wxformatarray($parameters = null, $urlencode = false){
if(is_null($parameters)){
$parameters = $this->parameters;
}
$restr = "";//初始化空
ksort($parameters);//排序参数
foreach ($parameters as $k => $v){//循环定制参数
if (null != $v && "null" != $v && "sign" != $k) {
if($urlencode){//如果参数需要增加url加密就增加,不需要则不需要
$v = urlencode($v);
}
$restr .= $k . "=" . $v . "&";//返回完整字符串
}
}
if (strlen($restr) > 0) {//如果存在数据则将最后“&”删除
$restr = substr($restr, 0, strlen($restr)-1);
}
return $restr;//返回字符串
}
/*******************************************************
* 微信md5签名生成器 - 需要将参数数组转化成为字符串[wxformatarray方法]
*******************************************************/
public function wxmd5sign($content, $privatekey){
try {
if (is_null($privatekey)) {
throw new exception("财付通签名key不能为空!");
}
if (is_null($content)) {
throw new exception("财付通签名内容不能为空");
}
$signstr = $content . "&key=" . $privatekey;
return strtoupper(md5($signstr));
}
catch (exception $e)
{
die($e->getmessage());
}
}
/*******************************************************
* 微信sha1签名生成器 - 需要将参数数组转化成为字符串[wxformatarray方法]
*******************************************************/
public function wxsha1sign($content){
try {
if (is_null($content)) {
throw new exception("签名内容不能为空");
}
//$signstr = $content;
return sha1($content);
}
catch (exception $e)
{
die($e->getmessage());
}
}
/*******************************************************
* 微信jsapi整合方法 - 通过调用此方法获得jsapi数据
*******************************************************/
public function wxjsapipackage(){
$jsapi_ticket = $this->wxjsapiticket();
// 注意 url 一定要动态获取,不能 hardcode.
$protocol = (!empty($_server['https']) && $_server['https'] !== 'off' || $_server['server_port'] == 443) ? "https://" : "http://";
$url = $protocol.$_server["http_host"].$_server["request_uri"];
$timestamp = time();
$noncestr = $this->wxnoncestr();
$signpackage = array(
"jsapi_ticket" => $jsapi_ticket,
"noncestr" => $noncestr,
"timestamp" => $timestamp,
"url" => $url
);
// 这里参数的顺序要按照 key 值 ascii 码升序排序
$rawstring = "jsapi_ticket=$jsapi_ticket&noncestr=$noncestr×tamp=$timestamp&url=$url";
//$rawstring = $this->wxformatarray($signpackage);
$signature = $this->wxsha1sign($rawstring);
$signpackage['signature'] = $signature;
$signpackage['rawstring'] = $rawstring;
$signpackage['appid'] = self::appid;
return $signpackage;
}
/*******************************************************
* 将数组解析xml - 微信红包接口
*******************************************************/
public function wxarraytoxml($parameters = null){
if(is_null($parameters)){
$parameters = $this->parameters;
}
if(!is_array($parameters) || empty($parameters)){
die("参数不为数组无法解析");
}
$xml = "<xml>";
foreach ($arr as $key=>$val)
{
if (is_numeric($val))
{
$xml.="<".$key.">".$val."</".$key.">";
}
else
$xml.="<".$key."><![cdata[".$val."]]></".$key.">";
}
$xml.="</xml>";
return $xml;
}
/*******************************************************
* 微信卡券:上传logo - 需要改写动态功能
*******************************************************/
public function wxcardupdateimg() {
$wxaccesstoken = $this->wxaccesstoken();
//$data['access_token'] = $wxaccesstoken;
$data['buffer'] = '@d:\\workspace\\htdocs\\yky_test\\logo.jpg';
$url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=".$wxaccesstoken;
$result = $this->wxhttpsrequest($url,$data);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
//array(1) { ["url"]=> string(121) "http://mmbiz.qpic.cn/mmbiz/ibuyxphqexepntw4atkyias1cf3ztkiars9pfpzf1k5icvxd7xw0kxuaxhdzkepd9miccmcn0dctjfw6tnm93miaafrq/0" }
}
/*******************************************************
* 微信卡券:获取颜色
*******************************************************/
public function wxcardcolor(){
$wxaccesstoken = $this->wxaccesstoken();
$url = "https://api.weixin.qq.com/card/getcolors?access_token=".$wxaccesstoken;
$result = $this->wxhttpsrequest($url);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
}
/*******************************************************
* 微信卡券:创建卡券
*******************************************************/
public function wxcardcreated($jsondata) {
$wxaccesstoken = $this->wxaccesstoken();
$url = "https://api.weixin.qq.com/card/create?access_token=" . $wxaccesstoken;
$result = $this->wxhttpsrequest($url,$jsondata);
$jsoninfo = json_decode($result, true);
return $jsoninfo;
}
/*******************************************************
* 微信卡券:jsapi 卡券package - 基础参数没有附带任何值 - 再生产环境中需要根据实际情况进行修改
*******************************************************/
public function wxcardpackage($cardid){
$timestamp = time();
$api_ticket = $this->wxjsapiticket();
$cardid = $cardid;
$arrays = array($api_ticket,$timestamp,$cardid);
sort($arrays);
$string = sha1(implode("",$arrays));
$resultarray['card_id'] = $cardid;
$resultarray['card_ext'] = array();
$resultarray['card_ext']['openid'] = 'oomn4s9miwqhsnnvpn0dbtu23toa';
$resultarray['card_ext']['timestamp'] = $timestamp;
$resultarray['card_ext']['signature'] = $string;
return $resultarray;
}
}
4. [代码]微信jsapi
<?php
require_once 'lib.inc.php';
$wx = new wxapi();
//通过网页获取openid
//if(!isset($_get['code'])){
// header("location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=".wxapi::appid."&redirect_uri=http://".$_server['server_name'].$_server['php_self']."&response_type=code&scope=snsapi_base&state=1#wechat_redirect");
//}
//else{
// $code = $_get['code'];
// $info = $wx->wxoauthaccesstoken($code);
//print_r($info);
// $openid = $info['openid'];
//}
////////////////////////////////////////////
$signpackage = $wx->wxjsapipackage();
//print_r($signpackage);
$kqinfo = $wx->wxcardpackage("");
$listinfo = $wx->wxcardlistpackage();
?>
<html>
<head>
<title>jsapi接口测试</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
</head>
<body>
<p>
<input type="button" id="batchaddcard" name="batchaddcard" value="添加卡券" /><br />
<input type="button" id="opencard" name="opencard" value="拉起卡券库" /><br />
<input type="button" id="sharetimeline" name="sharetimeline" value="分享朋友圈" /><br />
<p id="showinfo">
</p>
</p>
<script>
wx.config({
debug: false,
appid: '<?php echo $signpackage["appid"];?>',
timestamp: <?php echo $signpackage["timestamp"];?>,
noncestr: '<?php echo $signpackage["noncestr"];?>',
signature: '<?php echo $signpackage["signature"];?>',
jsapilist: [
// 所有要调用的 api 都要加到这个列表中
'onmenusharetimeline',
'onmenushareappmessage',
'addcard',
'opencard'
]
});
wx.ready(function () {
// 在这里调用 api
wx.onmenushareappmessage({
title: '互联网之子',
desc: '在长大的过程中,我才慢慢发现,我身边的所有事,别人跟我说的所有事,那些所谓本来如此,注定如此的事,它们其实没有非得如此,事情是可以改变的。更重要的是,有些事既然错了,那就该做出改变。',
link: 'http://movie.douban.com/subject/25785114/',
imgurl: 'http://demo.open.weixin.qq.com/jssdk/images/p2166127561.jpg',
trigger: function (res) {
// 不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回
alert('用户点击发送给朋友');
},
success: function (res) {
alert('已分享');
},
cancel: function (res) {
alert('已取消');
},
fail: function (res) {
alert(json.stringify(res));
}
});
document.queryselector('#sharetimeline').onclick = function () {
wx.onmenusharetimeline({
title: '互联网之子',
link: 'http://movie.douban.com/subject/25785114/',
imgurl: 'http://demo.open.weixin.qq.com/jssdk/images/p2166127561.jpg',
trigger: function (res) {
// 不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回
alert('用户点击分享到朋友圈');
},
success: function (res) {
alert('已分享');
},
cancel: function (res) {
alert('已取消');
},
fail: function (res) {
alert(json.stringify(res));
}
});
};
document.queryselector('#batchaddcard').onclick = function () {
wx.addcard({
cardlist: [
{
cardid: 'p7g0cj_1hgf2nijo4stlvtzawfhi',
cardext: '{"timestamp":"<?php echo $kqinfo['cardext']['timestamp'];?>", "signature":"<?php echo $kqinfo['cardext']['signature'];?>"}'
}
],
success: function (res) {
var cardlist = res.cardlist; // 添加的卡券列表信息
alert(cardlist);
},
cancel: function (res) {
alert('已取消');
},
fail: function (res) {
alert(json.stringify(res));
}
});
};
var sharedata = {
title: '微信js-sdk demo',
desc: '微信js-sdk,帮助第三方为用户提供更优质的移动web服务',
link: 'http://demo.open.weixin.qq.com/jssdk/',
imgurl: 'http://mmbiz.qpic.cn/mmbiz/ictdbqwnownrt8qia4lv7k3m9j1skqkcimxjct7j9rhyickdi45jrpbxdzdyrewnk0ia0n5tmnmfth7sdxtzmvvgxg/0'
};
wx.onmenushareappmessage(sharedata);
wx.onmenusharetimeline(sharedata);
});
var readyfunc = function onbridgeready() {
// 绑定关注事件
document.queryselector('#opencard').addeventlistener('click',
function(e) {
weixinjsbridge.invoke('choosecard', {
"app_id": "<?php echo $listinfo['app_id']?>",
"location_id ": '',
"sign_type": "sha1",
"card_sign": "<?php echo $listinfo['card_sign']?>",
"card_id": "<?php echo $listinfo['card_id']?>",
"card_type": "<?php echo $listinfo['card_type']?>",
"time_stamp": "<?php echo $listinfo['time_stamp']?>",
"nonce_str": "<?php echo $listinfo['nonce_str']?>"
},
function(res) {
alert(res.err_msg + res.choose_card_info);
$("#showinfo").empty().append(res.err_msg + res.choose_card_info);
});
});
}
if (typeof weixinjsbridge === "undefined") {
document.addeventlistener('weixinjsbridgeready', readyfunc, false);
} else {
readyfunc();
}
</script>
</body>
</html>
5. [代码]创建卡券
$kqinfo = array("card" => array());
$kqinfo['card']['card_type'] = 'general_coupon';
$kqinfo['card']['general_coupon'] = array('base_info' => array(), 'default_detail' => array());
$kqinfo['card']['general_coupon']['base_info']['logo_url'] = 'url';
$kqinfo['card']['general_coupon']['base_info']['code_type'] = 'code_type_qrcode';
$kqinfo['card']['general_coupon']['base_info']['brand_name'] = '';
$kqinfo['card']['general_coupon']['base_info']['title'] = '测试卡券';
$kqinfo['card']['general_coupon']['base_info']['color'] = 'color030';
$kqinfo['card']['general_coupon']['base_info']['notice'] = '测试测试测试';
$kqinfo['card']['general_coupon']['base_info']['description'] = '这是一张优惠券';
$kqinfo['card']['general_coupon']['base_info']['date_info']['type'] = 1;
$kqinfo['card']['general_coupon']['base_info']['date_info']['begin_timestamp'] = time();
$kqinfo['card']['general_coupon']['base_info']['date_info']['end_timestamp'] = time() + 100 * 24 * 3600;
$kqinfo['card']['general_coupon']['base_info']['sku']['quantity'] = 100000;
$kqinfo['card']['general_coupon']['default_detail'] = '测试数据\n测试数据\n测试数据';
//var_dump($kqinfo);
//$kqinfo = json_encode($kqinfo);
$kqinfo = c::enjson($kqinfo);
//print_r( $kqinfo);
//$resultdata = $wx->wxcardcreated($kqinfo);
相关推荐:
php restful api 示例
以上就是微信api接口的详细内容。