掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流
獲取小程序二維碼長鏈接,適用于需要的碼數(shù)量較多的業(yè)務場景。通過該接口生成的二維碼,永久有效,無數(shù)量限制。

POST https://openapi.baidu.com/rest/2.0/smartapp/qrcode/getunlimited?access_token=ACCESS_TOKEN
| 參數(shù)名 | 類型 | 是否必須 | 描述 |
|---|---|---|---|
| access_token | String | 是 | 接口調(diào)用憑證 |
| 參數(shù)名 | 類型 | 是否必須 | 默認值 | 示例 | 描述 |
|---|---|---|---|---|---|
| path | String | 否 | 主頁 | pages/index/index | 掃碼進入的小程序頁面路徑,最大長度 4000 字節(jié),可以為空。 |
| width | Int | 否 | 430 | 500 | 二維碼的寬度(單位:px)。最小 280px,最大 1280px |
| mf | Int | 否 | 1 | 1 | 是否包含二維碼內(nèi)嵌 logo 標識,1001 為不包含,默認包含 |
如果調(diào)用成功,直接返回圖片二進制內(nèi)容,如果請求失敗,會返回 JSON 格式的數(shù)據(jù)。
HTTP/1.1 200 OKContent-Type: image/png
HTTP/1.1 200 OKContent-Type : application/json
| 名稱 | 類型 | 描述 |
|---|---|---|
| errno | Int | 錯誤碼 |
| errmsg | String | 錯誤信息 |
| request_id | String | 請求 ID ,標識一次請求 |
| 錯誤碼 | 描述 |
|---|---|
| 110 | access_token 錯誤 |
| 400 | 輸入不合法(path 長度錯誤、width 長度錯誤) |
class Common_Qrcode{const URL_SEND_REG = 'https://openapi.baidu.com/rest/2.0/smartapp/qrcode/getunlimited?';/*** @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請求工具類* @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;}}

我們在微信上24小時期待你的聲音
解答本文疑問/技術咨詢/運營咨詢/技術建議/互聯(lián)網(wǎng)交流