微信小程序生成太陽碼php
https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=access_token
必須經過POST提交數據庫
並且參數必須是JSON的格式json
<?php /** * curl請求數據 * * @param string $url 請求地址 * @param array $param 請求參數 * @param string $contentType 請求參數格式(json) * @return boolean|mixed */ function https_request($url = '', $param = [], $contentType = ''){ $ch = curl_init(); // 請求地址 curl_setopt($ch, CURLOPT_URL, $url); // 請求參數類型 $param = $contentType == 'json' ? json_encode($param) : $param; // 關閉https驗證 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // post提交 if($param){ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $param); } // 返回的數據是否自動顯示 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 執行並接收響應結果 $output = curl_exec($ch); // 關閉curl curl_close($ch); return $output !== false ? $output : false; } $access_token="10_X1w4ypXMdfliiv4orym7n7Ur8UMV3LsPAyyBng-DOjcZfAW1mlfKb1BAvBQuBIMUvk_Bq2lv3E2TI8QLvTrnmy1TBxoDeXu_JvR_sobPBkUimmA-aNasktu-J6UWLCgAAAFUL"; $request_url='https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token; $request_data=array( 'scene'=>'abcdef', 'page'=>'pages/home/index', 'width'=>'690' ); print_r(https_request($request_url,$request_data,'json'));
這裏有幾點須要注意,page參數中的值必定要是小程序中存在的。小程序
這裏的access_token是用小程序的Appid和AppSecret生成的。以前還傻乎乎的去開啓公衆號的APPSecret。微信小程序
再一個,這裏返回的數據,不是JSON數據,而是二維碼圖片數據。api
如何經過postman操做呢?微信
萬能的POSTMAN啊。curl
{"page":"pages/home/index","scene":"abcdef","width":690}
封裝接口函數
// 獲取太陽碼 public function get_xcx_code() { $uid = $_POST['uid']; if (!$uid) { $this->json->setErr('10001', '缺乏參數'); $this->json->Send(); } // 獲取用戶信息 $user_model = M('user'); $user_info = $user_model->where(['id'=>$uid])->find(); if (!$user_info) { $this->json->setErr('10002', '用戶不存在'); $this->json->Send(); } $scene = $user_info['invite_code']; vendor('Func.Http'); $request_data = [ 'scene' => $scene, 'page' => "pages/home/index", 'width' => 690 ]; $request_url='https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$this->get_access_token(); $result = Http::doPostJson($request_url,$request_data); header('Content-Type: image/jpeg; charset=UTF-8'); echo $result;exit; }
處理成base64post
// 獲取太陽碼 public function get_xcx_code() { $uid = $_POST['uid']; if (!$uid) { $this->json->setErr('10001', '缺乏參數'); $this->json->Send(); } // 獲取用戶信息 $user_model = M('user'); $user_info = $user_model->where(['id'=>$uid])->find(); if (!$user_info) { $this->json->setErr('10002', '用戶不存在'); $this->json->Send(); } $scene = $user_info['invite_code']; vendor('Func.Http'); $request_data = [ 'scene' => $scene, 'page' => "pages/home/index", // 'width' => 690 ]; $request_url='https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$this->get_access_token(); $result = Http::doPostJson($request_url,$request_data); // ob_clean(); // header('Content-Type: image/png; charset=UTF-8'); // echo $result;exit; $this->json->setErr(0, '獲取成功'); $this->json->setAttr('data',base64_encode($result)); $this->json->Send(); }
封裝post請求
// 經過POST方式發送json數據 static public function doPostJson($url = '', $param = [] ,$contentType = 'json') { $ch = curl_init(); // 請求地址 curl_setopt($ch, CURLOPT_URL, $url); // 請求參數類型 $param = $contentType == 'json' ? json_encode($param) : http_build_query($param); // 關閉https驗證 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // post提交 if($param){ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $param); } // 返回的數據是否自動顯示 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 執行並接收響應結果 $output = curl_exec($ch); // 關閉curl curl_close($ch); return $output !== false ? $output : false; }
// 獲取太陽碼 public function get_xcx_code() { $uid = $_POST['uid']; if (!$uid) { $this->json->setErr('10001', '缺乏參數'); $this->json->Send(); } // 查看是否已存儲到數據庫 $user_suncode_model = M('user_suncode'); $user_suncode_info = $user_suncode_model->where(['uid'=>$uid])->find(); if ($user_suncode_info) { $this->json->setErr(0, '獲取成功'); $this->json->setAttr('data',$user_suncode_info['code_imgurl']); $this->json->Send(); } // 獲取用戶信息 $user_model = M('user'); $user_info = $user_model->where(['id'=>$uid])->find(); if (!$user_info) { $this->json->setErr('10002', '用戶不存在'); $this->json->Send(); } $scene = $user_info['invite_code']; vendor('Func.Http'); $request_data = [ 'scene' => $scene, 'page' => "pages/home/index", ]; $request_url='https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$this->get_access_token(); $result = Http::doPostJson($request_url,$request_data); // 存入cdn $cdn_result = $this->upload_cdn($result,'suncode'); if ($cdn_result['errno'] == 0) { // 存入數據庫 $add_data = [ 'uid' => $uid, 'code_imgurl' => $cdn_result['save_name'], 'addtime' => time() ]; $user_suncode_model = M('user_suncode'); $user_suncode_model->add($add_data); $this->json->setErr(0, '獲取成功'); $this->json->setAttr('data',$cdn_result['save_name']); $this->json->Send(); } else { $this->json->setErr(10099, '獲取失敗'); $this->json->Send(); } }
cdn函數
public function upload_cdn($img_result,$folders="common"){ $date_folders = date('Ymd',time()); $savePath = "site_upload/".$folders.'/'.$date_folders.'/';// 設置附件上傳目錄 if (!is_dir($savePath)){ @mkdir('./'.$savePath, 0777,true); } $file_name = time().'_'.mt_rand().".png"; $upload_flag = file_put_contents($savePath.$file_name,$img_result); if($upload_flag){ vendor('Qiniu.Qiniu'); $qiniu = new Qiniu(); $img = C('SF_HOST'). $savePath . $file_name; $ext = pathinfo($img, PATHINFO_EXTENSION); $name = time() . mt_rand() . '.' . $ext; $s = $qiniu->up($img, $name, C('QINIU.BUCKET')); if($s){ @unlink('./'.$savePath . $file_name); $res['errno']='0'; $res['errmsg']='ok'; $res['save_name'] = C('CDN.URI') . $name; }else{ @unlink('./' .$savePath . $file_name); $res['errno']='10001'; $res['errmsg']='上傳cdn失敗'; } }else{ $res['errno']='10002'; $res['errmsg']='上傳圖片失敗'; } return $res; }