php微信卡券创建、生成二维码等

<?php

ini_set('display_errors', 'On');
define('APPID', 'XXXXXXXXXX');
define('SECRET', 'XXXXXXXXXX');

function get_access_token(){
	$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".SECRET;
	$res = https_request($url, 'get', 'json');
	$access_token = $res["access_token"];
	return $access_token;
}

function https_request($url, $type="get", $res="json", $data = ''){
    //1.初始化curl
    $curl = curl_init();
    //2.设置curl的参数
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    if ($type == "post"){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    //3.采集
    $output = curl_exec($curl);
    //var_dump(curl_getinfo($curl));
    //4.关闭
    curl_close($curl);
    if ($res == 'json') {
        return json_decode($output,true);
    }
}

//上传图片素材(该方法只能获取到图片的URL)     2016-11-3
function addMaterialUrl() {
	$access_token = get_access_token();    
	$url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=".$access_token;
	$filename = 'test.png';  //文件名同目录下 其他目录自己指定
	$data = array("media"=>'@'. $filename);
	$res = https_request( $url ,'post', 'json', $data);
	print_r($res);
	return $res['url'];
}

function createCardMeg ($logo_url) {
	$access_token = get_access_token();    
	$url = "https://api.weixin.qq.com/card/create?access_token=".$access_token;
	$data = '{
  "card": {
      "card_type": "GROUPON",
      "groupon": {
          "base_info": {
              "logo_url":"'.$logo_url.'",
              "brand_name": "微信餐厅",
              "code_type": "CODE_TYPE_TEXT",
              "title": "132元双人火锅套餐",
              "color": "Color010",
              "notice": "使用时向服务员出示此券",
              "service_phone": "020-88888888",
              "description": "不可与其他优惠同享\n如需团购券发票,请在消费时向商户提出\n店内均可使用,仅限堂食",
              "date_info": {
                  "type": "DATE_TYPE_FIX_TIME_RANGE",
                  "begin_timestamp": 1514736000,
                  "end_timestamp": 1546272000
              },
              "sku": {
                  "quantity": 500000
              },
              "use_limit":100,
              "get_limit": 3,
              "use_custom_code": false,
              "bind_openid": false,
              "can_share": true,
              "can_give_friend": true,
              "location_id_list": [
                  123,
                  12321,
                  345345
              ],
              "center_title": "顶部居中按钮",
              "center_sub_title": "按钮下方的wording",
              "center_url": "www.qq.com",
              "custom_url_name": "立即使用",
              "custom_url": "http://www.qq.com",
              "custom_url_sub_title": "6个汉字tips",
              "promotion_url_name": "更多优惠",
              "promotion_url": "http://www.qq.com",
              "source": "大众点评"
          },
           "advanced_info": {
               "use_condition": {
                   "accept_category": "鞋类",
                   "reject_category": "阿迪达斯",
                   "can_use_with_other_discount": true
               },
               "abstract": {
                   "abstract": "微信餐厅推出多种新季菜品,期待您的光临",
                   "icon_url_list": [
                       "http://mmbiz.qpic.cn/mmbiz/p98FjXy8LacgHxp3sJ3vn97bGLz0ib0Sfz1bjiaoOYA027iasqSG0sj
  piby4vce3AtaPu6cIhBHkt6IjlkY9YnDsfw/0"
                   ]
               },
               "text_image_list": [
                   {
                       "image_url": "http://mmbiz.qpic.cn/mmbiz/p98FjXy8LacgHxp3sJ3vn97bGLz0ib0Sfz1bjiaoOYA027iasqSG0sjpiby4vce3AtaPu6cIhBHkt6IjlkY9YnDsfw/0",
                       "text": "此菜品精选食材,以独特的烹饪方法,最大程度地刺激食 客的味蕾"
                   },
                   {
                       "image_url": "http://mmbiz.qpic.cn/mmbiz/p98FjXy8LacgHxp3sJ3vn97bGLz0ib0Sfz1bjiaoOYA027iasqSG0sj piby4vce3AtaPu6cIhBHkt6IjlkY9YnDsfw/0",
                       "text": "此菜品迎合大众口味,老少皆宜,营养均衡"
                   }
               ],
               "time_limit": [
                   {
                       "type": "MONDAY",
                       "begin_hour":0,
                       "end_hour":10,
                       "begin_minute":10,
                       "end_minute":59
                   },
                   {
                       "type": "HOLIDAY"
                   }
               ],
               "business_service": [
                   "BIZ_SERVICE_FREE_WIFI",
                   "BIZ_SERVICE_WITH_PET",
                   "BIZ_SERVICE_FREE_PARK",
                   "BIZ_SERVICE_DELIVER"
               ]
           },
          "deal_detail": "以下锅底2选1(有菌王锅、麻辣锅、大骨锅、番茄锅、清补 凉锅、酸菜鱼锅可选):\n大锅1份 12元\n小锅2份 16元 "
      }
  }}';
	$res = https_request( $url ,'post', 'json', $data);
	print_r($res);
	return $res['card_id'];
}

function getQRTicket($card_id){
	$access_token = get_access_token();
	$data = ' {
		"action_name": "QR_CARD",
		"expire_seconds": 1800,
		"action_info": {
			"card": {
				"card_id": "'.$card_id.'",
				"code": "1234567890",
				"openid": "",
				"is_unique_code": false ,
				"outer_str":""
			}
		}
	}';
	$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
	$res = https_request( $url ,'post', 'json', $data);
	
    echo "<img src='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={$res['ticket']}' />";
	print_r($res);exit();
}

function wxCardWhiteList() {
	$access_token = get_access_token();
	$url = "https://api.weixin.qq.com/card/testwhitelist/set?access_token=".$access_token;
	$data = '{
		"openid": [
			"oG7Sh1R_j1bAmj_yz3jsAK5-Uep4"
		]
	}';
	$res = https_request( $url ,'post', 'json', $data);
	print_r($res);
}


$logo_url = addMaterialUrl();
$card_id = createCardMeg($logo_url);
wxCardWhiteList();
getQRTicket($card_id);

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信卡券API源码是指微信官方提供的用于开发者集成在自己的应用或网站中使用的代码库。通过使用这些源码,开发者可以实现在微信平台上创建和管理卡券、发放和核销卡券、查询卡券信息等功能。 微信卡券API源码主要包含了与卡券相关的前后端代码和接口文档。前端代码负责展示卡券、用户领取和使用卡券的界面,以及与后端进行数据交互。后端代码则负责处理卡券相关的业务逻辑,包括创建和修改卡券、发放和核销卡券、查询卡券信息等。 通过使用微信卡券API源码,开发者可以根据自己的需求进行二次开发和定制,实现与自己应用或网站的业务逻辑相匹配的卡券功能。开发者可以根据微信提供的接口文档了解源码中提供的接口和参数,根据需求进行相应的调用和调试。 微信卡券API源码的使用需要开发者具备一定的编程基础和对微信开发相关知识的了解。同时,开发者还需要在微信公众平台上注册开发者账号,并进行相关的配置和认证。在实际使用中,开发者还需要结合自己的应用场景和需求,进行相应的接口调用和业务逻辑的实现。 总之,微信卡券API源码是一个帮助开发者实现卡券功能的代码库,通过使用源码可以实现在微信平台上创建、管理和使用卡券的功能。开发者可以根据自己的需求进行二次开发和定制,实现与自己应用或网站的业务逻辑相匹配的卡券功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值