微信小程序生成太阳码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; }