十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
获取小程序二维码短链,长度固定 35 个字符,适用于需要的码数量较少的业务场景。通过该接口生成的二维码,永久有效,有数量限制。

POST https://openapi.baidu.com/rest/2.0/smartapp/qrcode/get?access_token=ACCESS_TOKEN
| 参数名 | 类型 | 是否必须 | 描述 |
|---|---|---|---|
| access_token | String | 是 | 接口调用凭证 |
| 参数名 | 类型 | 是否必须 | 默认值 | 示例 | 描述 |
|---|---|---|---|---|---|
| path | String | 否 | 主页 | pages/index/index | 扫码进入的小程序页面路径,最大长度 4000 字节,可以为空。 |
| width | Int | 否 | 430 | 500 | 二维码的宽度(单位:px)。最小 280px,最大 1280px |
| mf | Int | 否 | 1 | 1 | 是否包含二维码内嵌 logo 标识,1001 为不包含,默认包含 |
如果调用成功,会直接返回图片二进制内容,如果请求失败,会返回 JSON 格式的数据。
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/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;}}