13518219792

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

创新互联百度小程序教程:二维码短链

二维码短链

接口说明

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

接口地址

 
 
 
  1. 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 为不包含,默认包含

返回值说明

 
 
 
  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

返回信息

名称 类型 描述
errno Int 错误码
errmsg String 错误信息
request_id String 请求 ID,标识一次请求

错误码

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

Bug&Tip

请求示例

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

网站题目:创新互联百度小程序教程:二维码短链
文章分享:http://cdbrznjsb.com/article/cceiihe.html
  • 网站建设专属方案

  • 网站定制化设计

  • 7X24小时服务

  • N对管家服务

让你的专属顾问为你服务