日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

小程序二维码生成中的一些坑

發(fā)布時(shí)間:2024/3/26 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 小程序二维码生成中的一些坑 小編覺(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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。