新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)百度小程序教程:二維碼短鏈
- 二維碼短鏈
- 接口說明
- 接口地址
- 參數(shù)說明
- query 參數(shù)
- post 參數(shù)
- 返回值說明
- 正確情況下返回圖像的字節(jié)流,響應(yīng) header 中包含
- 錯(cuò)誤情況下返回
- 返回信息
- 錯(cuò)誤碼
- Bug&Tip
- 請(qǐng)求示例
二維碼短鏈
接口說明
獲取小程序二維碼短鏈,長(zhǎng)度固定 35 個(gè)字符,適用于需要的碼數(shù)量較少的業(yè)務(wù)場(chǎng)景。通過該接口生成的二維碼,永久有效,有數(shù)量限制。

接口地址
POST https://openapi.baidu.com/rest/2.0/smartapp/qrcode/get?access_token=ACCESS_TOKEN
參數(shù)說明
query 參數(shù)
| 參數(shù)名 | 類型 | 是否必須 | 描述 |
|---|---|---|---|
| access_token | String | 是 | 接口調(diào)用憑證 |
post 參數(shù)
| 參數(shù)名 | 類型 | 是否必須 | 默認(rèn)值 | 示例 | 描述 |
|---|---|---|---|---|---|
| path | String | 否 | 主頁 | pages/index/index | 掃碼進(jìn)入的小程序頁面路徑,最大長(zhǎng)度 4000 字節(jié),可以為空。 |
| width | Int | 否 | 430 | 500 | 二維碼的寬度(單位:px)。最小 280px,最大 1280px |
| mf | Int | 否 | 1 | 1 | 是否包含二維碼內(nèi)嵌 logo 標(biāo)識(shí),1001 為不包含,默認(rèn)包含 |
返回值說明
如果調(diào)用成功,會(huì)直接返回圖片二進(jìn)制內(nèi)容,如果請(qǐng)求失敗,會(huì)返回 JSON 格式的數(shù)據(jù)。
正確情況下返回圖像的字節(jié)流,響應(yīng) header 中包含
HTTP/1.1 200 OKContent-Type: image/png
錯(cuò)誤情況下返回
HTTP/1.1 200 OKContent-Type : application/json
返回信息
| 名稱 | 類型 | 描述 |
|---|---|---|
| errno | Int | 錯(cuò)誤碼 |
| errmsg | String | 錯(cuò)誤信息 |
| request_id | String | 請(qǐng)求 ID,標(biāo)識(shí)一次請(qǐng)求 |
錯(cuò)誤碼
| 錯(cuò)誤碼 | 描述 |
|---|---|
| 110 | access_token 錯(cuò)誤 |
| 400 | 輸入不合法(path 長(zhǎng)度錯(cuò)誤、width 長(zhǎng)度錯(cuò)誤) |
Bug&Tip
- Tip:POST 只支持 form 表單提交。
- Tip:接口只能生成已發(fā)布的小程序的二維碼 生成二維碼數(shù)量限制為 100,000 個(gè)
- Tip:正確返回 Content-Type:image/png
請(qǐng)求示例
- PHP
class Common_Qrcode{const URL_SEND_REG = 'https://openapi.baidu.com/rest/2.0/smartapp/qrcode/get?';/*** @desc 獲取 access_token* https://smartprogram.baidu.com/docs/develop/serverapi/power_exp/* @param $appkey* @param $appSecret* @return bool|string*/public static function getAccessToken($appkey, $appSecret) {$url = 'https://openapi.baidu.com/oauth/2.0/token?';$courierConf = Bd_Conf::getAppConf("classes/courier");$param = array("grant_type" => 'client_credentials',"client_id" => $appkey,"client_secret" => $appSecret,"scope" => 'smartapp_snsapi_base',);$url .= http_build_query($param);$curl = curl_init((string)$url);curl_setopt($curl, CURLOPT_HEADER, false);// 信任任何證書curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);$data = curl_exec($curl);curl_close($curl);$res = json_decode($data,1);if ($data === false || empty($res['access_token'])) {Bd_Log::warning("getToken fail! data[$data]");return false;}return $res['access_token'];}/*** @desc 獲取二維碼base64字節(jié)流* https://smartprogram.baidu.com/docs/develop/serverapi/get/* @param $path* @param $width* @return bool|string*/public static function getQrCode($path, $width){$accessToken = self::getAccessToken();if ($accessToken === false){return false;}$courierConf = Bd_Conf::getAppConf("classes/courier");$data = array("path" => $path,"width" => $width,"expire" => $expire,);$res = self::curlPost($data,$accessToken);return $res;}/*** curl POST請(qǐng)求工具類* @param array $postDataArr 傳遞的數(shù)組參數(shù)* @return string | array*/public static function curlPost($postDataArr,$accessToken){$headerArr = array("Content-type:application/x-www-form-urlencoded");$url = self::URL_SEND_REG;$param = array('access_token'=>$accessToken,);$url .= http_build_query($param);$curl = curl_init((string)$url);curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);curl_setopt($curl, CURLOPT_POST, 1);curl_setopt($curl, CURLOPT_POSTFIELDS, $postDataArr);curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArr);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);$output = curl_exec($curl);$info = curl_getinfo($curl);curl_close($curl);$res = '';if ($info["content_type"] == "image/png") {$res = base64_encode($output);}return $res;}}
網(wǎng)站題目:創(chuàng)新互聯(lián)百度小程序教程:二維碼短鏈
標(biāo)題URL:http://fisionsoft.com.cn/article/cceiihe.html


咨詢
建站咨詢
