生成二维码
生成二维码 API 文档
接口简介
本API提供二维码生成功能。支持生成PNG位图或SVG矢量格式的二维码,可自定义尺寸、纠错级别、边框宽度等参数。适用于需要集成二维码生成功能的Web应用、移动应用或第三方系统。请求说明
- 完整接口地址:https://api.xunjinlu.fun/api/qrcode/createqrcode.php
- 接口路径:/api/qrcode/createqrcode.php
- 请求方法:GET, POST
- 请求头:无特殊要求,可以使用通用HTTP请求头。
请求参数
| 参数名 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| ----------- | -------- | ---------- | ------------------------------------------------------------ |
| data | string | 是 | 二维码中包含的文本内容(URL、文本、联系方式等),必填,建议不超过900个字符 |
| size | string | 否 | 二维码尺寸,格式为'256'或'256x256',默认256x256像素 |
| format | string | 否 | 输出格式,可选值:png(位图,默认)、svg(矢量图) |
| error_correction | string | 否 | 纠错级别,可选值:L(7%)、M(15%,默认)、Q(25%)、H(30%) |
| border | integer | 否 | 边框宽度(以码点为单位),默认2,最小0 |
| raw | integer | 否 | 直接输出图片模式,1=直接输出图片流到浏览器,0或不传=返回JSON(默认) |
| download | integer | 否 | 下载模式,1=触发浏览器下载图片,0或不传=不下载(默认)。优先级低于raw |
| logo | string | 否 | Logo图片URL,会在二维码中心显示该图片 |
返回参数
| 参数名 | 类型 | 说明 |
|---|---|---|
| --------------- | ---------- | ------------------------------------------------------------ |
| code | integer | 状态码,200表示成功,其他错误码表示失败 |
| data | object | 返回的数据对象,包含二维码的图像信息等 |
| message | string | 状态信息,用于描述操作结果 |
| image_base64 | string | 二维码的Base64编码,可用于直接在网页上显示或嵌入到图片标签中 |
| image_url | string | 二维码图片的URL,可以直接访问图片文件 |
| api_url | string | 生成二维码的API请求URL,可用于直接访问API接口生成二维码图片 |
| format | string | 生成的二维码格式,如png或svg |
| size | string | 生成的二维码尺寸,如256x256 |
| data_content | string | 二维码中包含的文本内容 |
| error_correction | string | 二维码的纠错级别 |
| border | integer | 二维码的边框宽度 |
| direct_image_url | string | 直接输出图片的URL,用于直接显示或下载图片 |
返回示例
成功响应(默认Base64模式)
{
"code": 200,
"data": {
"image_base64": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...",
"image_url": "https://api.2dcode.biz/v1/create-qr-code?data=Example&size=256x256&format=png&error_correction=M&border=2",
"api_url": "https://api.xunjinlu.fun/api/qrcode/createqrcode.php",
"format": "png",
"size": "256x256",
"data_content": "Example",
"error_correction": "M",
"border": 2,
"direct_image_url": "https://api.xunjinlu.fun/api/qrcode/createqrcode.php?data=Example&raw=1&size=256x256&format=png"
},
"message": "success"
}
错误响应示例
{
"code": 400,
"data": null,
"message": "参数验证失败: data参数不能为空"
}
{
"code": 502,
"data": null,
"message": "二维码生成失败: HTTP Error: 503"
}
错误码说明
- 400:参数验证失败,请检查请求参数是否正确。
- 502:二维码生成失败,请稍后重试或联系技术支持。
调用示例
直接访问链接
https://api.xunjinlu.fun/api/qrcode/createqrcode.php?data=Example&size=256x256&format=png&error_correction=M&border=2cURL命令示例
curl -X POST "https://api.xunjinlu.fun/api/qrcode/createqrcode.php" -H "Content-Type: application/json" -d '{"data":"Example","size":"256x256","format":"png","error_correction":"M","border":"2"}'
JavaScript (fetch) 示例
fetch('https://api.xunjinlu.fun/api/qrcode/createqrcode.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
data: 'Example',
size: '256x256',
format: 'png',
error_correction: 'M',
border: '2'
})
}).then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
jQuery AJAX 示例
$.ajax({
url: 'https://api.xunjinlu.fun/api/qrcode/createqrcode.php',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
data: 'Example',
size: '256x256',
format: 'png',
error_correction: 'M',
border: '2'
}),
success: function(data) {
console.log(data);
},
error: function(error) {
console.error('Error:', error);
}
});
Python (requests) 示例
import requests
url = 'https://api.xunjinlu.fun/api/qrcode/createqrcode.php'
data = {
'data': 'Example',
'size': '256x256',
'format': 'png',
'error_correction': 'M',
'border': '2'
}
response = requests.post(url, json=data)
print(response.json())
PHP (curl) 示例
<?php
$url = 'https://api.xunjinlu.fun/api/qrcode/createqrcode.php';
$data = array(
'data' => 'Example',
'size' => '256x256',
'format' => 'png',
'error_correction' => 'M',
'border' => '2'
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
HTML (iframe/img/script标签)
<!-- iframe方式 -->
<iframe src="https://api.xunjinlu.fun/api/qrcode/createqrcode.php?data=Example&size=256x256&format=png&error_correction=M&border=2" width="256" height="256"></iframe>
<!-- img标签方式 -->
<img src="https://api.xunjinlu.fun/api/qrcode/createqrcode.php?data=Example&size=256x256&format=png&error_correction=M&border=2" width="256" height="256">
<!-- script标签方式(Base64编码) -->
<script>
fetch('https://api.xunjinlu.fun/api/qrcode/createqrcode.php?data=Example&size=256x256&format=png&error_correction=M&border=2')
.then(response => response.json())
.then(data => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const img = new Image();
img.src = 'data:image/png;base64,' + data.image_base64;
img.onload = () => {
canvas.width = 256;
canvas.height = 256;
ctx.drawImage(img, 0, 0, 256, 256);
document.body.appendChild(canvas);
};
});
</script>