港服(Server.HK)百度小程序教程:二维码短链

二维码短链 接口说明 接口地址 参数说明 query 参数 post 参数 返回值说明 正确情况下返回图像的字节流,响应…

  • 二维码短链
    • 接口说明
    • 接口地址
    • 参数说明
      • query 参数
      • post 参数
      • 返回值说明
      • 正确情况下返回图像的字节流,响应 header 中包含
      • 错误情况下返回
      • 返回信息
      • 错误码
      • Bug&Tip
      • 请求示例

    二维码短链

    接口说明

    获取小程序二维码短链,长度固定 35 个字符,适用于需要的码数量较少的业务场景。通过该接口生成的二维码,永久有效,有数量限制。

    接口地址

     
     
     
    1. POST https://openapi.baidu.com/rest/2.0/smartapp/qrcode/get?access_token=ACCESS_TOKEN

    参数说明

    query 参数

    参数名类型是否必须描述
    access_tokenString接口调用凭证

    post 参数

    参数名类型是否必须默认值示例描述
    pathString主页pages/index/index扫码进入的小程序页面路径,最大长度 4000 字节,可以为空。
    widthInt430500二维码的宽度(单位:px)。最小 280px,最大 1280px
    mfInt11是否包含二维码内嵌 logo 标识,1001 为不包含,默认包含

    返回值说明

     
     
     
    1. 如果调用成功,会直接返回图片二进制内容,如果请求失败,会返回 JSON 格式的数据。

    正确情况下返回图像的字节流,响应 header 中包含

     
     
     
    1. HTTP/1.1 200 OK
    2. Content-Type: image/png

    错误情况下返回

     
     
     
    1. HTTP/1.1 200 OK
    2. Content-Type : application/json

    返回信息

    名称类型描述
    errnoInt错误码
    errmsgString错误信息
    request_idString请求 ID,标识一次请求

    错误码

    错误码描述
    110access_token 错误
    400输入不合法(path 长度错误、width 长度错误)

    Bug&Tip

    • Tip:POST 只支持 form 表单提交。
    • Tip:接口只能生成已发布的小程序的二维码 生成二维码数量限制为 100,000 个
    • Tip:正确返回 Content-Type:image/png

    请求示例

    • PHP
     
     
     
    1. <?php
    2. class Common_Qrcode
    3. {
    4. const URL_SEND_REG = 'https://openapi.baidu.com/rest/2.0/smartapp/qrcode/get?';
    5. /**
    6. * @desc 获取 access_token
    7. * https://smartprogram.baidu.com/docs/develop/serverapi/power_exp/
    8. * @param $appkey
    9. * @param $appSecret
    10. * @return bool|string
    11. */
    12. public static function getAccessToken($appkey, $appSecret) {
    13. $url = 'https://openapi.baidu.com/oauth/2.0/token?';
    14. $courierConf = Bd_Conf::getAppConf("classes/courier");
    15. $param = array(
    16. "grant_type" => 'client_credentials',
    17. "client_id" => $appkey,
    18. "client_secret" => $appSecret,
    19. "scope" => 'smartapp_snsapi_base',
    20. );
    21. $url .= http_build_query($param);
    22. $curl = curl_init((string)$url);
    23. curl_setopt($curl, CURLOPT_HEADER, false);
    24. // 信任任何证书
    25. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    26. curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
    27. $data = curl_exec($curl);
    28. curl_close($curl);
    29. $res = json_decode($data,1);
    30. if ($data === false || empty($res['access_token'])) {
    31. Bd_Log::warning("getToken fail! data[$data]");
    32. return false;
    33. }
    34. return $res['access_token'];
    35. }
    36. /**
    37. * @desc 获取二维码base64字节流
    38. * https://smartprogram.baidu.com/docs/develop/serverapi/get/
    39. * @param $path
    40. * @param $width
    41. * @return bool|string
    42. */
    43. public static function getQrCode($path, $width)
    44. {
    45. $accessToken = self::getAccessToken();
    46. if ($accessToken === false){
    47. return false;
    48. }
    49. $courierConf = Bd_Conf::getAppConf("classes/courier");
    50. $data = array(
    51. "path" => $path,
    52. "width" => $width,
    53. "expire" => $expire,
    54. );
    55. $res = self::curlPost($data,$accessToken);
    56. return $res;
    57. }
    58. /**
    59. * curl POST请求工具类
    60. * @param array $postDataArr 传递的数组参数
    61. * @return string | array
    62. */
    63. public static function curlPost($postDataArr,$accessToken)
    64. {
    65. $headerArr = array(
    66. "Content-type:application/x-www-form-urlencoded"
    67. );
    68. $url = self::URL_SEND_REG;
    69. $param = array(
    70. 'access_token'=>$accessToken,
    71. );
    72. $url .= http_build_query($param);
    73. $curl = curl_init((string)$url);
    74. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    75. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    76. curl_setopt($curl, CURLOPT_POST, 1);
    77. curl_setopt($curl, CURLOPT_POSTFIELDS, $postDataArr);
    78. curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArr);
    79. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    80. $output = curl_exec($curl);
    81. $info = curl_getinfo($curl);
    82. curl_close($curl);
    83. $res = '';
    84. if ($info["content_type"] == "image/png") {
    85. $res = base64_encode($output);
    86. }
    87. return $res;
    88. }
    89. }

    为您推荐

    港服(Server.HK)百度小程序教程:grid 宫格

    grid 宫格 属性说明 示例 代码示例 grid 宫格 解释:宫格组件,包含上图下文和左图右文两种样式。可配置图片、主...

    港服(Server.HK)百度小程序教程:CanvasContext.setMiterLimit

    CanvasContext.setMiterLimit 方法参数 miterLimit 参数说明 示例 图片示例 代码示...

    港服(Server.HK)小程序教程:微信小程序API 视频解码器·VideoDecoder实例

    VideoDecoder 基础库 2.11.0 开始支持,低版本需做 兼容处理。 可通过 wx.createV...

    港服(Server.HK)小程序教程:微信小程序 WeUI·导航组件

    Navigation Navigation是小程序的顶部导航组件,当页面配置navigationStyle设置为cust...

    港服(Server.HK)百度小程序教程:CanvasContext.lineTo

    CanvasContext.lineTo 方法参数 示例 图片示例 代码示例 1 代码示例 2 CanvasContex...
    返回顶部