创新互联百度小程序教程:二维码短链
- 二维码短链
- 接口说明
- 接口地址
- 参数说明
- query 参数
- post 参数
- 返回值说明
- 正确情况下返回图像的字节流,响应 header 中包含
- 错误情况下返回
- 返回信息
- 错误码
- Bug&Tip
- 请求示例
二维码短链
接口说明
获取小程序二维码短链,长度固定 35 个字符,适用于需要的码数量较少的业务场景。通过该接口生成的二维码,永久有效,有数量限制。

接口地址
POST https://openapi.baidu.com/rest/2.0/smartapp/qrcode/get?access_token=ACCESS_TOKEN
参数说明
query 参数
| 参数名 | 类型 | 是否必须 | 描述 |
|---|---|---|---|
| access_token | String | 是 | 接口调用凭证 |
post 参数
| 参数名 | 类型 | 是否必须 | 默认值 | 示例 | 描述 |
|---|---|---|---|---|---|
| path | String | 否 | 主页 | pages/index/index | 扫码进入的小程序页面路径,最大长度 4000 字节,可以为空。 |
| width | Int | 否 | 430 | 500 | 二维码的宽度(单位:px)。最小 280px,最大 1280px |
| mf | Int | 否 | 1 | 1 | 是否包含二维码内嵌 logo 标识,1001 为不包含,默认包含 |
返回值说明
如果调用成功,会直接返回图片二进制内容,如果请求失败,会返回 JSON 格式的数据。
正确情况下返回图像的字节流,响应 header 中包含
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 长度错误) |
Bug&Tip
- Tip:POST 只支持 form 表单提交。
- Tip:接口只能生成已发布的小程序的二维码 生成二维码数量限制为 100,000 个
- Tip:正确返回 Content-Type:image/png
请求示例
- 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字节流* 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 传递的数组参数* @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;}}
分享标题:创新互联百度小程序教程:二维码短链
文章链接:http://www.jxjierui.cn/article/cceiihe.html


咨询
建站咨询
