会议门票是一种很常用的票据。使用微信卡券的会议门票功能,可以为参会者发放电子门票,设置座次,扫码签到及统计到访率等。
1 class class_wxcard 2 { 3 var $appid = APPID; 4 var $appsecret = APPSECRET; 5 6 // 构造函数,获取Access Token 7 public function __construct($appid = NULL, $appsecret = NULL) 8 { 9 if($appid && $appsecret){ 10 $this->appid = $appid; 11 $this->appsecret = $appsecret; 12 } 13 $res = file_get_contents('token.json'); 14 $result = json_decode($res, true); 15 $this->expires_time = $result["expires_time"]; 16 $this->access_token = $result["access_token"]; 17 if (time > ($this->expires_time + 3600)){ 18 $url = "https:// api.weixin.qq.com/cgi-bin/token?grant_type=client_ credential&appid=".$this->appid."&secret=".$this->appsecret; 19 $res = $this->http_request($url); 20 $result = json_decode($res, true); 21 $this->access_token = $result["access_token"]; 22 $this->expires_time = time; 23 file_put_contents('token.json', '{"access_token": "'.$this->access_ token.'", "expires_time": '.$this->expires_time.'}'); 24 } 25 } 26 27 // 上传图片 28 public function upload_image($file) 29 { 30 if (PHP_OS == "Linux"){ // Linux 31 $data = array("buffer" => "@".dirname(__FILE__).'/'.$file); 32 }else{ // WINNT 33 $data = array("buffer" => "@".dirname(__FILE__).'//'.$file); 34 } 35 $url = "https:// api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=".$this- >access_token; 36 $res = $this->http_request($url, $data); 37 return json_decode($res, true); 38 } 39 40 // 创建卡券 41 public function create_card($data) 42 { 43 $url = "https:// api.weixin.qq.com/card/create?access_token=".$this->access_token; 44 $res = $this->http_request($url, $this->array_to_json(array('card'=>$data))); 45 return json_decode($res, true); 46 } 47 48 // 创建二维码接口 49 public function create_card_qrcode($data) 50 { 51 $url = "https:// api.weixin.qq.com/card/qrcode/create?access_token=".$this- >access_token; 52 $res = $this->http_request($url, json_encode($data)); 53 return json_decode($res, true); 54 } 55 56 // 特殊票类 57 // 更新会议门票 58 public function update_card_meetingticket($data) 59 { 60 $url = "https:// api.weixin.qq.com/card/meetingticket/updateuser?access_ token=".$this->access_token; 61 $res = $this->http_request($url, $this->array_to_json($data)); 62 return json_decode($res, true); 63 } 64 65 // 多级数组转JSON(兼容中文、数字、英文、布尔型) 66 protected function array_to_json($array) 67 { 68 foreach ($array as $k => &$v) { 69 if (is_array($v)){ 70 foreach ($v as $k1 => &$v1) { 71 if (is_array($v1)){ 72 foreach ($v1 as $k2 => &$v2) { 73 if (is_array($v2)){ 74 foreach ($v2 as $k3 => &$v3) { 75 if (is_array($v3)){ 76 foreach ($v3 as $k4 => &$v4) { 77 $v3[$k4] = (is_string($v4))?urlencode ($v4):$v4; 78 } 79 }else{ 80 $v2[$k3] = (is_string($v3))?urlencode ($v3):$v3; 81 } 82 } 83 }else{ 84 $v1[$k2] = (is_string($v2))?urlencode($v2):$v2; 85 } 86 } 87 }else{ 88 $v[$k1] = (is_string($v1))?urlencode($v1):$v1; 89 } 90 } 91 // $this->array_to_json($v); 92 }else{ 93 $array[$k] = (is_string($v))?urlencode($v):$v; 94 } 95 } 96 return urldecode(json_encode($array)); 97 } 98 99 // HTTP请求(支持HTTP/HTTPS,支持GET/POST)100 protected function http_request($url, $data = null)101 {102 $curl = curl_init;103 curl_setopt($curl, CURLOPT_URL, $url);104 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);105 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);106 if (!empty($data)){107 curl_setopt($curl, CURLOPT_POST, 1);108 curl_setopt($curl, CURLOPT_POSTFIELDS, $data);109 }110 curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);111 $output = curl_exec($curl);112 curl_close($curl);113 return $output;114 }115 }
创建会议门票的代码如下。
1 // 创建会议/演出门票 2 $data = array('card_type' => "MEETING_TICKET", 3 'meeting_ticket' => array('base_info' => array('logo_url' => "http:// mmbiz.qpic.cn/mmbiz/K4LBh6RUO0qAa2sbY1EyJGDZ0eCetML4quMDRsiczNX9 8UPE6ryGM7ynjWCX1kibM5iaOLV5ibXHRhs8kdNoAthSjw/0", 4 'code_type' => "CODE_TYPE_BARCODE", 5 'brand_name' => "方倍票务公司", 6 'title' => "方倍会议门票", 7 'color' => "Color020", 8 'notice' => "使用时向检票员出示此券", 9 'description' => "请务必准时入场",10 'sku' => array('quantity' => 10000),11 'date_info' => array('type' => 1,12 'begin_timestamp' => time,13 'end_timestamp' => time + 2 * 86400),1415 ),16 'meeting_detail' => "地点:水立方",17 'map_url' => "http:// www.baidu.com/"18 ),19 );20 $result = $weixin->create_card($data);
生成卡券ID以后,使用该ID生成二维码进行投放,其代码如下。
1 // 创建二维码进行投放 2 $data = array('action_name' => "QR_CARD", 3 // 'expire_seconds' => 1800, 4 'action_info' => array('card' => array('card_id' => "piPuduNg0bM6 Q5hB8rrcK3OXavSE", 5 // 'code' => "198374613512", 6 // 'openid' => "oFS7Fjl0WsZ9AMZqrI80nbIq8xrA", 7 'is_unique_code' => false, 8 'outer_id' => 100), 910 ),11 );12 $result = $weixin->create_card_qrcode($data);
当用户领取门票以后,为其更新座次信息,代码如下。
1 // 更新会议门票2 $data = array(3 'code' => "019538767119",4 'card_id' => "piPuduNg0bM6Q5hB8rrcK3OXavSE",5 'zone' => "C区",6 'entrance' => "东北门",7 'seat_number' => "2排15号",8 );9 $result = $weixin->update_card_meetingticket($data);
最终用户领取到的门票及更新后的门票信息如图16-11所示。
图16-11 微信门票