微信開發-生成帶引數的二維碼及簡單使用
阿新 • • 發佈:2019-01-09
// 配置微信
function getWechatAccessToken(){ $appid = appid; $appsecret = appsecret; $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret; //獲取access_token $srt = $this->curl('POST',$url); $strjson=json_decode($srt); return $token = $strjson->access_token; }
// 獲取access_token
function getWechatAccessToken(){ $appid = appid; $appsecret = appsecret; $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appsecret; //獲取access_token $srt = $this->curl('POST',$url); $strjson=json_decode($srt); return $token = $strjson->access_token; }
// 獲取ticket 生成帶引數的二維碼 public function postTicket($id) { $accessToken = $this->getWechatAccessToken(); $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$accessToken;
//臨時二維碼 // $post_data = [ // "expire_seconds" => 604800,//時間為7天 此處是60*60*24*7// "action_name" => "QR_SCENE", // "action_info" => [ // "scene" => [ // "scene_id" => $id, // ] // ] // ];
$post_data = [ "action_name" => "QR_LIMIT_SCENE", "action_info" => [ "scene" => [ "scene_id" => $id, ] ] ]; $dat = json_encode($post_data); $str = json_decode($this->curl('POST',$url,$dat)); // $ticket = $str->ticket; // return response()->json(['code'=>'200','msg'=>'ok','data'=>$ticket]); return response()->json(['code'=>200,'msg'=>'ok','data'=>$str]); }
// 根據ticket 生成二維碼地址
https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=TICKET// 使用帶引數的二維碼
$postStr = file_get_contents("php://input"); // 獲取微信在回撥地址傳回來的資料 \Log::info(json_encode($postStr)); // 列印log日誌 $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); // 把xml格式轉換成字串 $fromUsername = (string)$postObj->FromUserName; // 傳送方帳號(一個OpenID) 掃碼者openId $event = (string)$postObj->Event; // 獲取型別 $EventKey = trim((string)$postObj->EventKey); // 事件KEY值 // $data = User::where('openId',$fromUsername)->first(); if ($event == 'subscribe' || $event == 'SCAN'){ if (!empty($EventKey)){ if (starts_with($EventKey,'qrscene_')){ $EventKey = substr($EventKey,8);// 擷取 } $fatherId = User::where('openId',$EventKey)->first(); $a = User::where('scope', '=', 'user')->get()->max('id'); if (empty($a)) { $b = "QXZ00001"; } else { $bb = User::where('id', $a)->first()->invitation; $b = ++$bb; } $user = new User(); $user->father_id = $fatherId->id; // 父級id $user->scope = 'user'; $user->type = 'fans'; //使用者型別 $user->invitation = $b; // 隨機的邀請碼 // 隨機的邀請碼 $user->openId = $fromUsername; if ($user->save()) {return response()->json(['code' => 200, 'msg' => '註冊成功']); } else { return response()->json(['code' => 500, 'msg' => '註冊失敗']); } } }