這里給大家分享一下我在使用小程序時(shí)遇到的一個(gè)功能實(shí)現(xiàn)方法,生成二維碼,這里需要注意以下幾點(diǎn): 1.小程序調(diào)用生成二維碼成功的時(shí)候,返回的是一個(gè)二維碼圖片的二進(jìn)制文件流。 'data:' . $mime . ';base64,'base64_encode($wx_result), 如果需要瀏覽器可以直接查看返回的二進(jìn)制流,拼接成 <image src="'data:' . $mime . ';base64,'base64_encode($wx_result)" >,直接瀏覽器可以查看。 代碼如下: /* * 生成活動(dòng)分享頁二維碼*/ public function buildImgIndex() { try{ $unionid = post('unionid'); $fileName = $unionid.'.png'; $fileData = WidRead($fileName); if($fileData){ echo $this->img_uri($fileData);exit; }else{ $TokenData = getAccessToken(); if($TokenData['code'] == 200){ $accessToken = $TokenData['data']['access_token']; }else{ echo jsonOut(returnArr(410,'','error'));exit; } $page = "pages/activityflower/activityflower"; $scene = "unionid=".$unionid; $path = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$accessToken; $parameter = array( "access_token" => $accessToken, 'scene' => $scene, //綁定該用戶的unionid 'page' => $page, 'auto_color'=>false, 'width'=>430 ); $parameter = json_encode($parameter); $curl = new \Curl\Curl(); $curl->setHeader('content-type','application/json; charset=utf-8'); $curl->setOpt(CURLOPT_SSL_VERIFYPEER,false); $curl->setOpt(CURLOPT_SSL_VERIFYHOST,false); $curl->post($path,$parameter); if($curl->error){ $error_message = $curl->error_message; $error_code = $curl->error_code; $response = $curl->response_headers; $param['response'] = $response; $param['code'] = $error_code; $param['msg'] = $error_message; $param['contents'] = "coin connect error"; $curl->close(); echo jsonOut(returnArr(400,'','')); }else{ $data = $curl->response; //返回圖片文件流 $curl->close(); } $dataJson = json_decode($data);//如果成功返回文件流 json_decode為null,其他就自帶微信的錯(cuò)誤返回 if($dataJson == null){ WidWrite($fileName,$data); echo $this->img_uri($data);exit; }else{ echo jsonOut(returnArr(400,'',''));exit; } } }catch(\Exception $e){ $res = array("code" => $e->getCode(), 'msg' => $e->getMessage()); echo jsonOut(returnArr(400, $res, 'error'));exit; } } /* * 二進(jìn)制轉(zhuǎn)圖片image/png * */ public function img_uri($contents, $mime ="image/png") { $base64 = base64_encode($contents); return ('data:' . $mime . ';base64,' . $base64); }
|
|