ホームページ  >  記事  >  php教程  >  PHP 微信卡券

PHP 微信卡券

PHP中文网
PHP中文网オリジナル
2016-05-22 18:27:001863ブラウズ

微信卡券创建,删除等

1. [代码][PHP]代码 

<?php
/**
 * Created by ${COMPANY}.
 * User: msli
 * Date: 14-7-21
 * Time: 上午11:40
 * Purpose: 模型类
 * To change this template use File | Settings | File Templates.
 */
namespace Home\Model;
use Think\Model;
use Think\Cache;


class TestModel extends Model
{
   /*  /*
    * 获取 所有奖项信息
    */

    public function index($openid){
          /*
          $adapter = D("Julymoviecodes");
          $where[&#39;openid&#39;] = $openid;
          $info = $adapter->where($where)->find();
          return $info;
          */
    }

    public function requesturl($url,$method){
    
    	$ch = curl_init($url);
    	if($method==&#39;GET&#39;){
    		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, &#39;GET&#39;);
    	}else{
    		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, &#39;POST&#39;);
    	}
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    	curl_setopt($ch, CURLOPT_USERAGENT, &#39;Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20100101 Firefox/21.0&#39;);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    	$info = curl_exec($ch);
    
    	$dataJson = json_decode($info, true);
        
    	return $dataJson;
    }
    
    
    /*请求URL,返回 ACCESS_TOKEN*/
    public function get_access_token(){
    	$APPID = C("APPID");
    	$APPKEY = C("SECRET");
    	$ACCESS_TOKEN = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPKEY."";
    	
    	$accesstokenResult = $this->requesturl($ACCESS_TOKEN,&#39;POST&#39;);
    	$accesstoken = $accesstokenResult[&#39;access_token&#39;];
    	
    	return $accesstoken;
    }
    
    
	//////获取wtw 的 access_token
	public function get_wtw_Token(){

    	/////微信公众平台  access_token
    	$time =time()*1000;
    	$appid = 15;
    	$token ="b4384c050f27b99151501b1a95eb529c";
  	
    	$tmpArr = array($token, $time, $appid);
    	sort($tmpArr,SORT_STRING);
    	$tmpStr = implode($tmpArr);
    	$signature = sha1($tmpStr);	
       
    	$userinfourl ="http://weixin.augmarketing.com/wechatconnect/app/accessToken"."?appId=".$appid."&timestamp=".$time."&signature=".$signature."";    	
    	$userinfo = $this->requesturl($userinfourl,"GET");
		///dump($userinfo);
    	return $userinfo[&#39;data&#39;][&#39;accessToken&#39;];
	}

	/*
	 * 获取wtw 的 创建开卡接口()
	 * 
	 * string(68) "{"errcode":0,"errmsg":"ok","card_id":"pPLOfjlZ0kuDE55Pz-CJgImR-YnI"}"	
	 * 创建开卡成功,但是无法在微信后台显示?????????
	 *
	 */
	public function  get_cardadd(){
		//获取ACCESS_TOKEN
		$ACCESS_TOKEN = $this->get_wtw_Token();
		$data = &#39;{
					"card":{
						"card_type":"GROUPON",
						"groupon":{
							"base_info":{	
								"logo_url": "http://www.supadmin.cn/uploads/allimg/120216/1_120216214725_1.jpg",
								"brand_name":"海底捞", 
								"code_type":0,
								"title":"132元双人火锅套餐",
								"sub_title":"双人火锅套餐",
								"color":"Color010",
								"notice": "使用时向服务员出示此券",
								"service_phone":"020-88888888",
								"description":"不可与其他优惠同享\n如需团购券发票,请在消费时向商户提出\n店内均可使用,仅限堂食\n餐前不可打包,餐后未吃完,可打包\n本团购券不限人数,建议2人使用,超过建议人数须另收酱料费5元/位\n本单谢绝自带酒水饮料",
								"date_info":{
									"type":1,
									"begin_timestamp":1407577600,
									"end_timestamp":1419910400
								},
								"sku":{
									"quantity": 50000000
								},
								"use_limit":1,
								"get_limit":3,
								"use_custom_code":true,
								"bind_openid":false,
								"can_share":true,
								"url_name_type":1
							},
							"deal_detail":"以下锅底2选1(有菌王锅、麻辣锅、大骨锅、番茄锅、清补凉锅、酸菜鱼锅可选):鲜菇猪肉滑1份18元\n金针菇1份16元\n黑木耳1份9元\n娃娃菜1份8元\n欢乐畅饮2位 12元\n自助酱料2位10元",
						}
					}
				}&#39;;
		
		$add_url ="https://api.weixin.qq.com/card/add?access_token=".$ACCESS_TOKEN."";
		$info = $this->wtw_request($add_url,$data);
		dump($info);
	}

	/*
	* 获取wtw 的 创建  会员卡 开卡接口()
	*string(68) "{"errcode":0,"errmsg":"ok","card_id":"pPLOfjsx3Qc46vYbX4A-pbo0FywA"}"
	* 创建开卡成功,但是无法在微信后台显示?????????
	*
	*/
	public function  get_memcardadd($openid){
		//获取ACCESS_TOKEN
		$ACCESS_TOKEN = $this->get_wtw_Token();
		$memadd_url ="https://api.weixin.qq.com/card/add?access_token=".$ACCESS_TOKEN."";
        $sub_title = $this->get_cardidByOpenID($openid);
		////激活链接
		$activate_url ="https://wx.blinq.cn/cptest/Diamond/index.php/index/indextest?wechat_card_js=1";
		
		$data = &#39;{
					"card":{
						"card_type":"MEMBER_CARD",
						"member_card":{
							"base_info":{
								"logo_url": "http://www.blinq.cn/Public/i/i.png",
								"brand_name":"缤刻",
								"code_type":0,
								"title":"缤刻缤缤卡会员",
								"sub_title":"卡号:&#39;.$sub_title.&#39;",
								"color":"Color010",
								"notice": "使用时向服务员出示此券",
								"service_phone":"020-88888888",
								"description":"不可与其他优惠同享\n如需团购券发票,请在消费时向商户提出\n店内均可使用,仅限堂食\n餐前不可打包,餐后未吃完,可打包\n本团购券不限人数,建议2人使用,超过建议人数须另收酱料费5元/位\n本单谢绝自带酒水饮料",
								"date_info":{
									"type":1,
									"begin_timestamp":1407577600,
									"end_timestamp":1419910400
								},
								"sku":{
									"quantity": 50000000
								},
								"use_limit":1,
								"get_limit":3,
								"use_custom_code":false,
								"bind_openid":true,
								"openid":"&#39;.$openid.&#39;",
								"can_share":false
							},
							"supply_bonus":false,
							"supply_balance":false,
							"bonus_cleared":"不可转用其他人",
							"init_bonus":0,
				            "init_balance":0,
							"prerogative":"不可与其他优惠同享\n如需团购券发票,请在消费时向商户提出\n店内均可使用",
							"bind_old_card_url":"&#39;.$activate_url.&#39;"
						}
					}
				}&#39;;
		$info = $this->wtw_request($memadd_url,$data);
		$card_id = json_decode($info,true);
        return  $card_id["card_id"];
	}

    /*
    * 生成会员卡的 二维码
    *
    * {"errcode":40053,"errmsg":"invalid action info, please check document"}
    */
    public function get_member_qrcode(){
        //获取ACCESS_TOKEN
        $ACCESS_TOKEN = $this->get_wtw_Token();
        $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$ACCESS_TOKEN."";
        $data = &#39;{
					"action_name":"QR_CARD",
					"action_info":{
						"card": {
							"card_id":"pPLOfjgeq4G41xlsVeqDD4__wJlo",
                            "openid": "oPLOfjvlD4LTKP-8WpVotQJPVtNY",
                            "expire_seconds": "1800" ,
                            "is_unique_code": false
							},
						 "use_custom_code":true,
					 }
				 }&#39;;

        $qrcoderes= $this->wtw_request($url,$data);
        dump($qrcoderes);
        $qrcoderes=json_decode($qrcoderes,true);
        //$qrcodeurl = &#39;https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=&#39;. urlencode($qrcoderes[&#39;ticket&#39;]);
        ///echo "<img src=&#39;https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={$qrcoderes[&#39;ticket&#39;]}&#39; />";
        //return $qrcoderes;
    }

	/*
	 *
	* 微信通过 调用API激活会员卡
	*/
	public function active_member(){
		$ACCESS_TOKEN = $this->get_wtw_Token();
		$active_url ="https://api.weixin.qq.com/card/membercard/activate?access_token=".$ACCESS_TOKEN."";
		/*
		$data =&#39;{
					"init_bonus":100,
					"init_balance":100,
					"membership_number":"1823554",
					"code":"798912843155",
					"card_id":"pPLOfjhDHao6ZUFYUZMfhwiluPgM"
				}&#39;;
	  	
		$member_info = $this->wtw_request($active_url,$data);
		dump($member_info);*/
	}

	/*
	 * 会员卡编号及积分信息
	 */
	public function get_memberinfo(){
	}
	
	/*
	 * 获取 的 创建  电影票 开卡接口()
	*  创建电影票成功
	*  string(68) "{"errcode":0,"errmsg":"ok","card_id":"pPLOfjtTWd0LH97lQOrfLQPcfjUI"}"
	*/
	public function  get_movieticketadd(){
		//获取ACCESS_TOKEN
		$ACCESS_TOKEN = $this->get_wtw_Token();
		$data = &#39;{
					"card":{
						"card_type":"MOVIE_TICKET",
						"movie_ticket":{
							"base_info":{	
								"logo_url": "http://www.blinq.cn/upload/images/activityImages/2014/7/24/14/2014072414192526572vh_615&460.jpg",
								"brand_name":"秦时明月", 
								"code_type":0,
								"title":"秦时明月之龙腾万里",
								"sub_title":"秦时明月之龙腾万里",
								"color":"Color010",
								"notice": "使用时向服务员出示此券",
								"service_phone":"020-88888888",
								"description":"不可与其他优惠同享",
								"date_info":{
									"type":1,
									"begin_timestamp":1407577600,
									"end_timestamp":1419910400
								},
								"sku":{
									"quantity": 50000000
								},
								"use_limit":1,
								"get_limit":3,
								"use_custom_code":true,
								"bind_openid":false,
								"can_share":true,
								"url_name_type":1
							},
							"deal_detail":"改编自拥有500万粉丝的中国第一人气武侠动画系列剧《秦时明月》,即将于2013年暑期上映",
						}
					}
				}&#39;;
		
		$add_url ="https://api.weixin.qq.com/card/add?access_token=".$ACCESS_TOKEN."";
		
		$info = $this->wtw_request($add_url,$data);
		dump($info);
	}

	/*
	* 拉取门店列表
	* 
	* "errcode":0,"errmsg":"ok","location_list":[{"id":526,"name":"烈士陵园","phone":"","address":"地铁1号线","longitude":113.285392761,"latitude":23.1267681122}
	*/
	public function  get_storelist(){
		//获取ACCESS_TOKEN
		$ACCESS_TOKEN = $this->get_wtw_Token();
		$url = "https://api.weixin.qq.com/card/location/batchget?access_token=".$ACCESS_TOKEN."";
		$data =&#39;{
					"offset":0,
					"count":50
				}&#39;;
		$storelist = $this->wtw_request($url,$data);
		return $storelist;
	}
	
	/*
	 * 获取颜色列表接口
	 * 
	 * {"errcode":0,"errmsg":"ok","colors":[{"name":"Color010","value":"#55bd47"},{"name":"Color020","value":"#10ad61"}
	 */
	public function get_colorslist(){
		//获取ACCESS_TOKEN
		$ACCESS_TOKEN = $this->get_wtw_Token();
		$url = "https://api.weixin.qq.com/card/getcolors?access_token=".$ACCESS_TOKEN."";
		$data = null;
		$colorslist = $this->wtw_request($url,$data);
		return $colorslist;
	}
	
	/*
	 * 生成卡券二维码
	 * 
	 * {"errcode":40053,"errmsg":"invalid action info, please check document"}
	 */
	public function get_qrcode(){
		//获取ACCESS_TOKEN
		$ACCESS_TOKEN = $this->get_wtw_Token();
		$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$ACCESS_TOKEN."";
		
		 $data = &#39;{
					"action_name":"QR_CARD", 
					"action_info":{
						"card": {
							"card_id":"pPLOfjhDHao6ZUFYUZMfhwiluPgM", 
							"is_unique_code":false,
							},
						 "use_custom_code":true,
					 } 
				 }&#39;;
	
		$qrcoderes= $this->wtw_request($url,$data);
		$qrcoderes=json_decode($qrcoderes,true);
		$qrcodeurl = &#39;https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=&#39;. urlencode($qrcoderes[&#39;ticket&#39;]);
		
		echo "<img src=&#39;https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket={$qrcoderes[&#39;ticket&#39;]}&#39; />";
		//return $qrcoderes;
	
	}

	/*
	 * 添加到 卡包 weixinJS
	 * 
	 */
	
	/*
	 * 卡券核销部分
	 * 该卡已核销
	 * 
	 * "string(117) "{"errcode":0,"errmsg":"ok","card":{"card_id":"pPLOfjhDHao6ZUFYUZMfhwiluPgM"},"openid":"oPLOfjvlD4LTKP-8WpVotQJPVtNY"}"
	 */
	public function get_carddestroy($code =0 ,$card_id=0){
        //$code = 656268747314  ;
        //$card_id ="pPLOfjhDHao6ZUFYUZMfhwiluPgM";
		$ACCESS_TOKEN = $this->get_wtw_Token();
		$url = "https://api.weixin.qq.com/card/code/consume?access_token=".$ACCESS_TOKEN."";
		
		$data = &#39;{
					"code":"&#39;.$code.&#39;",
					"card_id":&#39;.$card_id.&#39;
				}&#39;;
		
		$card_del= $this->wtw_request($url,$data);
		return $card_del;
	}
	
	/*
	 * 会员卡交易(卡券支持优惠券(代金券、折扣券、礼品券、团购券)、会员卡、景点门票、门票、电影票)
	*
	* "errcode":40071,"errmsg":"invalid card type"}//////因为暂时 没有	开通会员卡
	*/
	public function get_membertrade(){
		//获取ACCESS_TOKEN
		$ACCESS_TOKEN = $this->get_wtw_Token();
		$url = "https://api.weixin.qq.com/card/membercard/updateuser?access_token=".$ACCESS_TOKEN."";
		
		$data = &#39;{
					"code":"798912843155",
					"card_id":"pPLOfjhDHao6ZUFYUZMfhwiluPgM",
					"record_bonus":"消费30元,获得3积分",
					"add_bonus":3,
					"add_balance":-300,
					"record_balance":"购买焦糖玛琪朵一杯,扣除金额30元"
				}&#39;;
		
		$membercard_update= $this->wtw_request($url,$data);
		dump($membercard_update);
	}
	
	/*
	 * 领取电影票后通过调用更新电影票接口update电影信息及用户选座信息
	*{"errcode":0,"errmsg":"ok"}"
	*/
	public function get_movietrade(){
		//获取ACCESS_TOKEN
		$ACCESS_TOKEN = $this->get_wtw_Token();
		$url = "https://api.weixin.qq.com/card/movieticket/updateuser?access_token=".$ACCESS_TOKEN."";
		$data = &#39;{
					"code":"277217129962",
					"card_id":"p1Pj9jr90_SQRaVqYI239Ka1erkI",
					"ticket_class":"4D",
					"show_time":1408493192,
					"screening_room":"5号影厅",
					"seat_number":"5排14号"
				}&#39;;
		$movie_update= $this->wtw_request($url,$data);
		dump($movie_update);
	}
	
	/*
	 * 拉起卡券列表(chooseCard)JSAPI,weixinJS
	 * 
	 */

	/*
	 * code解码接口
	* "{"errcode":0,"errmsg":"ok","code":"992718526867"}"/////反向推导,这个Code是正确的;
	*/
	public function get_codedecrypt(){
		$ACCESS_TOKEN = $this->get_wtw_Token();
		$url = "https://api.weixin.qq.com/card/code/decrypt?access_token=".$ACCESS_TOKEN."";
	
		$data =&#39;{
					"encrypt_code":"XXIzTtMqCxwOaawoE91+VJdsFmv7b8g0VZIZkqf4GWA60Fzpc8ksZ/5ZZ0DVkXdE"
				}&#39;;
		$decrypt= $this->wtw_request($url,$data);
		dump($decrypt);
	}
	
	
	/*
	* code 加密 接口
	*
	* "{"errcode":0,"errmsg":"ok","code":"992718526867"}"/////反向推导,这个Code是正确的;
	*/
	public function get_codeencrypt(){
		$ACCESS_TOKEN = $this->get_wtw_Token();
		
		//$decrypt= $this->wtw_request($url,$data);
		//dump($decrypt);
	}
	
	
	/*
	 * 卡券管理---删除卡券
	 * 
	 * string(27) "{"errcode":0,"errmsg":"ok"}"
	 */
	public function get_carddelete(){
		$ACCESS_TOKEN = $this->get_wtw_Token();
		$url = "https://api.weixin.qq.com/card/delete?access_token=".$ACCESS_TOKEN."";
		
		$data =&#39;{
					"card_id":"pPLOfjtTWd0LH97lQOrfLQPcfjUI"
				}&#39;;
		$delete= $this->wtw_request($url,$data);	
		dump($delete);
	}
	
	/*
	 * 查询Code
	 * 
	 * {"errcode":40056,"errmsg":"invalid serial code"}"
	 */
	public function get_codeget(){
		//获取ACCESS_TOKEN
		$ACCESS_TOKEN = $this->get_wtw_Token();
		$url = "https://api.weixin.qq.com/card/code/get?access_token=".$ACCESS_TOKEN."";
		
		$data =&#39;{
					"code":"798912843155"
				}&#39;;
		$codeget= $this->wtw_request($url,$data);
		dump($codeget);
		
		$data = json_decode($codeget,true);
		$openid = $data["openid"];
		
		$userinfo = $this->get_subscribe_res($openid);
		$result = $this->get_opencard_res("13761029787");
	}
	
	/*
	 * 批量查询卡列表
	 *
	 *{"errcode":0,"errmsg":"ok","card_id_list":["pPLOfjiKhDAvmDmjN0wQvolGHtbg","pPLOfjhDHao6ZUFYUZMfhwiluPgM"],"total_num":2}"
	 */
	public function get_cardgetall(){
		//获取ACCESS_TOKEN
		$ACCESS_TOKEN = $this->get_wtw_Token();
        //dump($ACCESS_TOKEN);
		$url = "https://api.weixin.qq.com/card/batchget?access_token=".$ACCESS_TOKEN."";
		$data =&#39;{
					"offset": 0, 
					"count":10
				}&#39;;
		$cardgetall= $this->wtw_request($url,$data);
		//dump($cardgetall);
        return $cardgetall;
	}
	
	/*
	 * 查询卡券详情
	*
	* {"errcode":0,"errmsg":"ok","card":{"card_type":"GENERAL_COUPON","general_coupon":{"base_info":{"id":"pPLOfjhDHao6ZUFYUZMfhwiluPgM","logo_url":"http:\/\/m
	*/
	public function get_carddetail(){
		//获取ACCESS_TOKEN
		$ACCESS_TOKEN = $this->get_wtw_Token();
		$url = "https://api.weixin.qq.com/card/get?access_token=".$ACCESS_TOKEN."";
	
		$data =&#39;{
				"card_id":"pPLOfjhDHao6ZUFYUZMfhwiluPgM"
				}&#39;;
		$carddetail= $this->wtw_request($url,$data);
		dump(json_decode($carddetail));
	}
	
	/*
	 * 事件推送
	 * 
	 * {"errcode":0,"
	 */
	public function get_event(){
		$ACCESS_TOKEN = $this->get_wtw_Token();
		///$carddetail= $this->wtw_request($url,$data);
		$toUser = "oPLOfjqw1fKtdUQPk1vDXCf7K8s0";
		$FromUser ="oPLOfjqw1fKtdUQPk1vDXCf7K8s0";
		$time = time();
		$cardid ="pPLOfjhDHao6ZUFYUZMfhwiluPgM";
		$textTpl ="<xml>
						<ToUserName><![CDATA[".$toUser."]]></ToUserName>  
						<FromUserName><![CDATA[.$FromUser.]]></FromUserName> 
						<CreateTime>".$time."</CreateTime>
						<MsgType><![CDATA[event]]></MsgType>
						<Event><![CDATA[card_pass_check]]></Event>
						<CardId><![CDATA[".$cardid."]]></CardId>
						<IsGiveByFriend>1</IsGiveByFriend>
						<UserCardCode><![CDATA[12312312]]></UserCardCode> 
					</xml>";
		
	}
	/*
	* 更改 Code
	*
	* {"errcode":0,"code 无效
	*/
	public function get_codeupdate(){
		//获取ACCESS_TOKEN
		$ACCESS_TOKEN = $this->get_wtw_Token();
		dump($ACCESS_TOKEN);
		$update_url = "https://api.weixin.qq.com/card/code/update?access_token=".$ACCESS_TOKEN."";
		
		$data =&#39;{
					"code":"151322901878",
					"card_id":"pPLOfjhDHao6ZUFYUZMfhwiluPgM",
			    	"new_code":"151322901879",
				}&#39;;
		/*
		$codeupdate = $this->wtw_request($update_url,$data);
		dump($codeupdate);
		*/
	}
	
	/*
	 * 卡券失效接口 
	*
	*  {"errcode":0,"code无效
	*/
	public function get_codeunable(){
		//获取ACCESS_TOKEN
		$ACCESS_TOKEN = $this->get_wtw_Token();
		$unable_url = "https://api.weixin.qq.com/card/code/unavailable?access_toke=".$ACCESS_TOKEN."";
	
		$data =&#39;{
					"code":"151322901878",
					"card_id":"pPLOfjhDHao6ZUFYUZMfhwiluPgM"
				}&#39;;
		/*
		$codeunable= $this->wtw_request($unable_url,$data);
		dump($codeunable);
		*/
	}
	
	/*
	* 添加白名单(果子的 )
	*
	*  
	*/
	public function get_whitelist(){
		//获取ACCESS_TOKEN
		$ACCESS_TOKEN = $this->get_wtw_Token();
		$white_url = "https://api.weixin.qq.com/card/testwhitelist/set?access_toke=".$ACCESS_TOKEN."";
	
		$data =&#39;{				
					"openid":[
							  "oPLOfjksQLgxSPDiL2wSSYWDA_ZU"
						]		
				}&#39;;
		/*
		$whileresult= $this->wtw_request($white_url,$data);
		dump($whileresult);	
		*/
	}

	
    /*请求URL,判断用户是否已经关注*/
    public function get_subscribe_res($openid){
  
    	/////微信公众平台
    	$time =time()*1000;
    	$appid = 15;
    	$token ="b4384c050f27b99151501b1a95eb529c";
  	
    	$tmpArr = array($token, $time, $appid);
    	sort($tmpArr,SORT_STRING);
    	$tmpStr = implode($tmpArr);
    	$signature = sha1($tmpStr);	
       
    	$userinfourl ="http://weixin.augmarketing.com/wechatconnect/app/users/property/".$openid."?appId=".$appid."&timestamp=".$time."&signature=".$signature."";    	
    	$userinfo = $this->requesturl($userinfourl,"GET");
    	///dump($userinfo);
    	return $userinfo;
    }
    /*
     * 
     * 得到签名Signature 的查询字符串
     */
    public function getSortQueryString($params = array(), $exceptKeys = array())
    {
    	$querySting = &#39;&#39;;
    	ksort($params);
    	foreach ($params as $key => $value) {
    		if(!in_array($key, $exceptKeys)) {
    			$querySting .= self::urlencode($key) . &#39;=&#39; . self::urlencode($value) . &#39;&&#39;;
    		}
    	}
    	return mb_substr($querySting, 0, mb_strlen($querySting) - 1);
    }
    /*
     * URLencode加密字符串
     */
    public static function urlencode($string = &#39;&#39;)
    {
    	return str_replace(&#39;~&#39;, &#39;%7E&#39;, rawurlencode($string));
    }
    /*
     * URLDecode解密字符串
     */
    public static function urldecode($string = &#39;&#39; )
    {
    	return str_replace(&#39;%20&#39;, &#39;+&#39;, str_replace(&#39;%2A&#39;, &#39;*&#39;, rawurldecode($string)));
    }
    
    public function signature($params = array(), $token)
    {
    	$queryString = $this->getSortQueryString($params, array(&#39;sig&#39;));
    	return urlencode(base64_encode(hash_hmac(&#39;sha256&#39;, $queryString, $token, true)));
    }
    
    /*
     * 调用开卡接口
     */
    public  function get_opencard_res($telephone){
    	$ts = strtotime(&#39;now&#39;);
    	///$telephone ="18621383387";
    	$appkey =&#39;3ccdb900&#39;;
    	$secret ="957a8ca7a73c4d14d070fbb819eb864a";
    	////Sig签名生成需要secret,appkey 3ccdb900对应的secret是:957a8ca7a73c4d14d070fbb819eb864a;$signature = urlencode(base64_encode(hash_hmac("sha256","app_key=urlencoded(".$appkey.")&check=urlencoded(1)&ts=urlencoded(".$time.")",$secret,true)));
    	$params = array(
                    &#39;app_key&#39; => $appkey,
                    &#39;ts&#39; => $ts,
                    &#39;uuid&#39; => $telephone,
    			);
    	$sig = $this->signature($params,$secret);
    	
    	$opencardurl ="http://mmapi.blinq.augmarketing.com/benefit/list?app_key=3ccdb900&ts=".$ts."&uuid=".$telephone."&sig=".$sig."";
    	$opencardinfo = $this->requesturl($opencardurl, "GET");
    	
    	//dump($opencardinfo);
    	if(!empty($opencardinfo)){
    		if(!empty($opencardinfo["data"])){
    			return 1;
    		}else{
    			return 0;
    		}
    	}else{
    		return 0;
    	}
    }
    

    /*请求URL,通过 当前微信号openid 获取用户member_id */
    public function get_member_id($openid){
    
    	$ts = strtotime(&#39;now&#39;);
    	///$telephone ="18621383387";
    	$appkey =&#39;3ccdb900&#39;;
    	$secret ="957a8ca7a73c4d14d070fbb819eb864a";
    	////Sig签名生成需要secret,appkey 3ccdb900对应的secret是:957a8ca7a73c4d14d070fbb819eb864a;$signature = urlencode(base64_encode(hash_hmac("sha256","app_key=urlencoded(".$appkey.")&check=urlencoded(1)&ts=urlencoded(".$time.")",$secret,true)));
    	$params = array(
                    &#39;app_key&#39; => $appkey,
                    &#39;ts&#39; => $ts,
    				&#39;attribute_id&#39; => 9,
    				&#39;attribute_value&#39;=>$openid
    			);
    	$sig = $this->signature($params,$secret);
    	
    	$member_url ="http://mmapi.blinq.augmarketing.com/member/list?app_key=3ccdb900&ts=".$ts."&sig=".$sig."&attribute_id=9&attribute_value=$openid";
    	$member_info = $this->requesturl($member_url, "GET");
    	dump($member_info);
    	return $member_info["data"]["items"][0]["member_id"];
    	
    }

    /*请求URL,通过 当前微信号openid 获取用户 微生活卡号 member_id */
    public function get_member_cardid($member_id=669937){
       
    	$ts = strtotime(&#39;now&#39;);
    	///$telephone ="18621383387";
    	$appkey =&#39;3ccdb900&#39;;
    	$secret ="957a8ca7a73c4d14d070fbb819eb864a";
    	////Sig签名生成需要secret,appkey 3ccdb900对应的secret是:957a8ca7a73c4d14d070fbb819eb864a;$signature = urlencode(base64_encode(hash_hmac("sha256","app_key=urlencoded(".$appkey.")&check=urlencoded(1)&ts=urlencoded(".$time.")",$secret,true)));
    	$params = array(
    			&#39;app_key&#39; => $appkey,
    			&#39;ts&#39; => $ts,
    			&#39;member_id&#39; => $member_id
    	);
    	$sig = $this->signature($params,$secret);
    	 
    	$member_url ="http://mmapi.blinq.augmarketing.com/member/detail?app_key=3ccdb900&ts=".$ts."&sig=".$sig."&member_id=$member_id";
    	$member_info = $this->requesturl($member_url, "GET");
    	dump($member_info);
    	//return $member_info["data"]["card_number"];
    	///dump($member_info);	 
    }
    ////获取用户 微生活卡号
    public function get_cardidByOpenID($openid){
    	$cardid =  $this->get_member_id($openid);
    	return $this->get_member_cardid($cardid);
    }
    // 允许
    public function allowbenefit($telephone){
    	$Cache = Cache::getInstance(&#39;Redis&#39;);
    	$key = "diamond_benefit:get_benefit_value_".$telephone ;
    	$times = $Cache->get($key);
 
    	if (empty($times) || ($times==false)){
    		$value = $this->get_opencard_res($telephone);
    		$Cache->set($key,$value);
    		return $value;
    	}
    	return $times;
    }
    /*
	 * Curl 请求
	 */
    public function wtw_request($url,$data=null){
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER[&#39;HTTP_USER_AGENT&#39;]); // 模拟用户使用的浏览器
        if($data != null){
            curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        }
        curl_setopt($curl, CURLOPT_TIMEOUT, 300); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $info = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo &#39;Errno:&#39;.curl_getinfo($curl);//捕抓异常
            dump(curl_getinfo($curl));
        }
        return $info;
    }

    /*
     * 模板消息
     */
    public function templetemsg(){
        //获取ACCESS_TOKEN
        /*string(86) "8jvpKfJAiEMSNQF-Ht2lAQklCJqND6AMYkU1ZPujIEa1Tl45lGSxoCybkymjCgbg0FdYbeF6s4A7Q41kpHZnAg"
         *  string(45) "{"errcode":0,"errmsg":"ok","msgid":201652404}"
         */
        $ACCESS_TOKEN = $this->get_wtw_Token();
        dump($ACCESS_TOKEN);
        $msg_url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$ACCESS_TOKEN."";
        $time = date("Y-m-d H:i:s",time());
        $url ="http://www.blinq.cn/";   ////这个链接是点击图文 跳转的链接,换行只能用\n 不能用<Br/>
        ////请求包为一个json:
        $msg_json= &#39;{
                        "touser":"oPLOfjqw1fKtdUQPk1vDXCf7K8s0",
                        "template_id":"iZOHjFSa-Ax2UuDlzjJx6IrwUrSvdDhD4TQPnM8kFSY",
                        "url":"&#39;.$url.&#39;",
                        "topcolor":"#FF0000",
                        "data":{
                            "first":{
                                "value":"尊敬的李先生:\n恭喜你模板接口调用成功了\n\n",
                                "color":"#000000"
                            },
                            "keyword1":{
                                "value":"模板接口调用测试\n\n",
                                "color":"#000000"
                            },
                            "keyword2":{
                                "value":"&#39;.$time.&#39;\n\n",
                                "color":"#000000"
                            },
                            "remark":{
                                "value":"\n\nThis message is intended only for the use of the inpidual or entity激活成功",
                                "color":"#0099FF"
                            }
                        }
                    }&#39; ;
       $result =  $this->wtw_request($msg_url,$msg_json);
        ///$result = $this->msg_request($msg_url, $msg_json, $header = array());
        dump($result);
    }
}

                   

                   

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。