南昌定制網(wǎng)站公司百度一下首頁官網(wǎng)百度
獲取小程序碼:小程序碼為圓圖,有數(shù)量限制。
目錄
文檔
接口地址
功能描述
注意事項
請求參數(shù)
對接
獲取小程序碼
調(diào)用獲取
小程序碼示例
總結(jié)
文檔
接口地址
https://api.weixin.qq.com/wxa/getwxacode?access_token=access_token
功能描述
該接口用于獲取小程序碼,適用于需要的碼數(shù)量較少的業(yè)務(wù)場景。通過該接口生成的小程序碼,永久有效,有數(shù)量限制,詳見獲取小程序碼。
注意事項
如果調(diào)用成功,會直接返回圖片二進(jìn)制內(nèi)容,如果請求失敗,會返回 JSON 格式的數(shù)據(jù)。
POST 參數(shù)需要轉(zhuǎn)成 JSON 字符串,不支持 form 表單提交。
與 createQRCode 總共生成的碼數(shù)量限制為 100,000,請謹(jǐn)慎調(diào)用。
已生成碼數(shù)量參考HTTP Header的Num-Used。
請求參數(shù)
返回參數(shù)
對接
獲取小程序碼
對獲取小程序碼方法進(jìn)行了封裝。
代碼如下:
/*** 獲取小程序碼 -- 適用于需要的碼數(shù)量較少的業(yè)務(wù)場景(永久有限 有數(shù)量限制)* @param $page* @param $access_token* @return false|string*/
public function getQRCode($page, $access_token)
{$url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" . $access_token;$data = json_encode(['path' => $page,'width' => 430,]);$options = ['http' => ['header' => "Content-type: application/json",'method' => 'POST','content' => $data,],];$context = stream_context_create($options);return file_get_contents($url, false, $context);
}
調(diào)用獲取
傳遞參數(shù)給獲取小程序碼函數(shù)。
與獲取不受限制的小程序碼的區(qū)別在于可以把小程序路徑和參數(shù)一起傳給page即可。
代碼如下:
$image = $this->getQRCode('pages/home/goodsdetail/Goodsdetail?goods_id=1546', $access_token);
if (strlen($image) < 1000) {$res = json_decode($image, true);ds_json_encode(10001, '錯誤', [$res->errcode, $res->errmsg]);
}
$filename = sprintf('./uploads/min_%d.png', time());
file_put_contents($filename, $image);ds_json_encode(10000, ['path' => $filename]);
小程序碼示例
總結(jié)
獲取不受數(shù)量限制的小程序碼適用于需要推廣的鏈接較多且路徑或者參數(shù)比較靈活的場景。