使用PHP实现微信摇一摇周边红包(5)

{ "errcode":0, "errmsg":"", "repeat_ticket_list":[ { "ticket": "v1|ZiPs2l0hpMBp3uwGI1rwp45vOdz/V/zQ/00jP9MeWT+e47/q1FJjwCIP34frSjzOxAEzJ7k2CtAg1pmcShvkChBWqbThxPm6MBuzceoHtj79iHuHaEn0WAO+j4sXnXnbGswFOlDYWg1ngvrRYnCY3g==" }, { "ticket":"v1|ZiPs2l0zzXCsdfwe45dxCdHiukOdz/V/zQ/89xcnC5XnT+e47/q1FJjwCO4frSjzOxAEzJ7k2CtAg1pmcShvkChBWzc45dDGC32Dcxx4DGxczjDCGsdjowe9iHuaEn0WAO+GswFOlDYWg1ngvrRYnCY3g==" } } ], "success_num":100 } /** * 摇一摇红包 录入红包 * @author jiosen */ class lottery_pub extends Wxpay_client_pub { var $code;//code码,用以获取openid var $openid;//用户的openid function __construct($access_token) { //设置接口链接 $this->url = "https://api.weixin.qq.com/shakearound/lottery/setprizebucket?access_token=".$access_token; //设置curl超时时间 $this->curl_timeout = WxPayConf_pub::CURL_TIMEOUT; } /** * 生成接口参数 json */ function createJson() { try { //检测必填参数 if($this->parameters["lottery_id"] == null) { throw new SDKRuntimeException("缺少抽奖活动id lottery_id !"."<br>"); }else if(empty($this->parameters["prize_info_list"])){ throw new SDKRuntimeException("缺少抽奖活动红包 prize_info_list !"."<br>"); } $this->parameters["mchid"] = WxPayConf_pub::MCHID;//授权商户号 $this->parameters["sponsor_appid"] = WxPayConf_pub::APPID;//授权上号appid return json_encode($this->parameters); //echo json_encode($this->parameters);die; }catch (SDKRuntimeException $e) { die($e->errorMessage()); } } function setJsonArray($parameter, $parameterValue){ $this->parameters[$this->trimString($parameter)] = $parameterValue; } function hbpreorder() { $data = $this->createJson(); $result = $this->curl_post($this->url,$data); $result = json_decode($result); return $result; } function curl_post($url,$data) { $curl = curl_init($url); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($curl, CURLOPT_TIMEOUT, 10); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_POST, 1);//发送一个常规的Post请求 curl_setopt($curl, CURLOPT_POSTFIELDS, $data);//Post提交的数据包 $rv = curl_exec($curl);//输出内容 curl_close($curl); return $rv; } /** * 作用:生成可以获得code的url */ function createOauthUrlForCode($redirectUrl) { $urlObj["appid"] = WxPayConf_pub::APPID; $urlObj["redirect_uri"] = "$redirectUrl"; $urlObj["response_type"] = "code"; $urlObj["scope"] = "snsapi_base"; $urlObj["state"] = "STATE"."#wechat_redirect"; $bizString = $this->formatBizQueryParaMap($urlObj, false); return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString; } /** * 作用:生成可以获得openid的url */ function createOauthUrlForOpenid() { $urlObj["appid"] = WxPayConf_pub::APPID; $urlObj["secret"] = WxPayConf_pub::APPSECRET; $urlObj["code"] = $this->code; $urlObj["grant_type"] = "authorization_code"; $bizString = $this->formatBizQueryParaMap($urlObj, false); return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString; } /** * 作用:通过curl向微信提交code,以获取openid */ function getOpenid() { $url = $this->createOauthUrlForOpenid(); //初始化curl $ch = curl_init(); //设置超时 curl_setopt($ch, CURLOP_TIMEOUT, $this->curl_timeout); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //运行curl,结果以jason形式返回 $res = curl_exec($ch); curl_close($ch); //取出openid $data = json_decode($res,true); $this->openid = $data['openid']; return $this->openid; } /** * 作用:设置code */ function setCode($code_) { $this->code = $code_; } }

 调用

<br>    $token = getAccessToken();<br>    $Redpack = new \lottery_pub($token);<br>    $lottery_id = ''; //这里读取数据库取出创建活动时返回的 lottery_id $Redpack->setParameter('lottery_id', $lottery_id); //活动id $prize_info_list =array(array('ticket'=>'这里取出预下单返回的sp_ticket')); $Redpack->setJsonArray('prize_info_list', $prize_info_list); //提交 $Redpack->hbpreorder();

抢红包页面  php

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/75d9b5f998dce52e62154c54866bf4e3.html