小程序二维码生成中的一些坑
生活随笔
收集整理的這篇文章主要介紹了
小程序二维码生成中的一些坑
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
小程序二維碼生成接口:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/qr-code.html
官方提供了三種接口,我這里使用的是B類接口,遇到的幾個(gè)坑都是通用的。
1、access_token應(yīng)放在url中,不在post傳遞的參數(shù)中
在這里,如果你僅把a(bǔ)ccess_token放在post傳遞的參數(shù)中或url和post傳參中都放,會(huì)報(bào)錯(cuò)access_token失效或數(shù)據(jù)格式錯(cuò)誤
2、scene不能為空,隨意填寫就行,但不能為空
方案一(將二維碼存儲(chǔ)在本地):
public function getQRcode(){$access_token = json_decode(file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->AppID.'&secret='.$this->AppSecret),true)['access_token'];$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$access_token";$ch = curl_init();$data = json_encode(['scene' => 'test']);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_HEADER, 'image/gif');curl_setopt($ch, CURLOPT_URL,$url);curl_setopt($ch, CURLOPT_POSTFIELDS,$data);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//跳過(guò)ssl檢測(cè)curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //如果需要將結(jié)果直接返回到變量里,那加上這句。$res = curl_exec($ch);file_put_contents('test.png',$res);$this->smarty_lib->tmp("getQRcode.html",'');}方案二(直接渲染):
public function getQRcode(){$access_token = json_decode(file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->AppID.'&secret='.$this->AppSecret),true)['access_token'];$url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=$access_token";$ch = curl_init();$data = json_encode(['scene' => 'test']);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_HEADER, 'image/gif');curl_setopt($ch, CURLOPT_URL,$url);curl_setopt($ch, CURLOPT_POSTFIELDS,$data);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data)));curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //如果需要將結(jié)果直接返回到變量里,那加上這句。$res = curl_exec($ch);$data = 'data:image/jpeg;base64,'.base64_encode($res);//補(bǔ)全base64加密字符串頭$html = "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title>二維碼</title></head><body><img src='$data'></body></html>";echo $html;exit;}- note:微信接口返回的是二進(jìn)制圖片,所以需要先處理一番再渲染
總結(jié)
以上是生活随笔為你收集整理的小程序二维码生成中的一些坑的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Linux下ifconfig中没有wla
- 下一篇: 机器学习入门:第十五章 递归神经网络