在线调试
填写业务相关参数免费在线调试
输入任意银行(中文名 / 英文 slug 均可),秒级返回该银行下全部 30 个币种的最新现汇买卖价、现钞买卖价、中间价、折算价与各币种更新时间。统一字段命名 + 自动数值清洗("-" 转 null),可直接驱动客户端"银行牌价大盘"表格视图。10 分钟本地缓存,命中毫秒级,覆盖 19 家主流银行。
全站会员->单独包月->次数包->点数计费->账户余额->免费额度
全站会员有效期内,可免费使用本产品。
| 免费/测试用户请求频率限制 | 请求频率总限制 | 全站会员请求频率限制 | 每日请求次数总限制 |
|---|---|---|---|
| 3秒1次 每个免费用户的QPS总限制 | 1秒1次 每个用户QPS总限制 | 1秒10次 每个全站会员用户的QPS总限制 | 10000 (次) 每个用户每日请求总次数 |
接口地址:https://daoyouapi.com/api/aab/index
返回格式:application/json
请求方式:HTTPGET
请求示例:https://daoyouapi.com/api/aab/index?key=你的密钥&bank=看文档
| 名称 | 值 |
|---|---|
| Content-Type | application/x-www-form-urlencoded;charset:utf-8; |
| 名称 | 必填 | 类型 | 示例值 | 说明 |
|---|---|---|---|---|
| key | 是 | string | 35kj5jnlj53453kl5j43nj5 | 接口密钥,在控制台->密钥管理查看 |
| bank | 是 | string | boc | 银行 slug 或中文名(如:boc、ICBC、中国银行、招商),共 19 家 |
| 名称 | 类型 | 说明 |
|---|---|---|
| code | int | 状态码,200=成功 |
| success | bool | 是否成功 |
| message | string | 状态信息 |
| data.engine | string | 引擎标识 |
| data.note | string | 价格单位与字段说明 |
| data.bank | object | 银行基础信息 |
| data.bank.slug | string | 银行 slug |
| data.bank.name | string | 银行中文名 |
| data.bank.tel | string | 客服电话 |
| data.bank.website | string | 银行官网地址 |
| data.global_updated_at | string | 页面整体更新时间 |
| data.count | int | 本次返回的币种数 |
| data.list | array | 各币种牌价清单 |
| data.list[].currency_slug | string | 币种 slug |
| data.list[].currency_name | string | 币种中文名 |
| data.list[].currency_code | string | 币种 ISO 代码 |
| data.list[].cash_buy | float/null | 现汇买入价 |
| data.list[].cash_sell | float/null | 现汇卖出价 |
| data.list[].cash_mid | float/null | 现汇中间价 |
| data.list[].note_buy | float/null | 现钞买入价 |
| data.list[].note_sell | float/null | 现钞卖出价 |
| data.list[].note_mid | float/null | 现钞中间价 |
| data.list[].middle_price | float/null | 中行公布中间价 |
| data.list[].conversion_price | float/null | 折算价 |
| data.list[].updated_at | string | 该币种更新时间 |
| debug | string/array | 调试数据 |
| exec_time | float | 执行耗时(秒) |
| user_ip | string | 客户端IP |
| timestamp | int | 服务器时间戳 |
本服务仅供个人学习、学术研究目的使用,未经授权,请勿分享、传播输入及生成的文本、图片内容。用户在从事与本服务相关的一切行为(包括但不限于访问浏览、利用、转载、宣传介绍)时,应以善意且谨慎的态度行事并承担相应的风险和责任。用户需确保不得利用本服务进行危害国家安全和社会公共利益、扰乱经济秩序和社会秩序、侵犯他人合法权益等法律、行政法规禁止的活动,并且自定义输入文本不得包含违反法律法规、政治相关、侵犯他人合法权益的内容。对于用户因使用本服务而导致的任何损失或风险,包括但不限于由于使用本服务而产生的错误、疏忽、中断、删除、故障、延迟或通讯线路故障所导致的任何损失或损害,本服务的开发者将不承担任何责任。
{}
- "code": 200,
- "success": true,
- "message": "查询成功",
-"data": {},
-"bank": {},
- "slug": "boc",
- "name": "中国银行",
- "tel": "95566",
- "website": "https://www.boc.cn/"
- "global_updated_at": "2026-05-17 18:55:25",
- "count": 27,
-"list": [],
-{},
- "currency_slug": "usd",
- "currency_name": "美元",
- "currency_code": "USD",
- "cash_buy": 6.8046,
- "cash_sell": 6.8333,
- "cash_mid": null,
- "note_buy": 6.8046,
- "note_sell": 6.8333,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 6.8415,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "eur",
- "currency_name": "欧元",
- "currency_code": "EUR",
- "cash_buy": 7.8977,
- "cash_sell": 7.9555,
- "cash_mid": null,
- "note_buy": 7.8977,
- "note_sell": 7.9555,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 7.9534,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "gbp",
- "currency_name": "英镑",
- "currency_code": "GBP",
- "cash_buy": 9.0527,
- "cash_sell": 9.1199,
- "cash_mid": null,
- "note_buy": 9.0527,
- "note_sell": 9.1199,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 9.1349,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "hkd",
- "currency_name": "港币",
- "currency_code": "HKD",
- "cash_buy": 0.8689,
- "cash_sell": 0.8723,
- "cash_mid": null,
- "note_buy": 0.8689,
- "note_sell": 0.8723,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 0.8734,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "mop",
- "currency_name": "澳门币",
- "currency_code": "MOP",
- "cash_buy": 0.8435,
- "cash_sell": 0.8474,
- "cash_mid": null,
- "note_buy": 0.8435,
- "note_sell": 0.8474,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 0.8476,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "jpy",
- "currency_name": "日元",
- "currency_code": "JPY",
- "cash_buy": 0.042768,
- "cash_sell": 0.043098,
- "cash_mid": null,
- "note_buy": 0.042768,
- "note_sell": 0.043098,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 0.043126,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "aud",
- "currency_name": "澳元",
- "currency_code": "AUD",
- "cash_buy": 4.8586,
- "cash_sell": 4.8972,
- "cash_mid": null,
- "note_buy": 4.8586,
- "note_sell": 4.8972,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 4.9244,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "cad",
- "currency_name": "加元",
- "currency_code": "CAD",
- "cash_buy": 4.9416,
- "cash_sell": 4.9804,
- "cash_mid": null,
- "note_buy": 4.9416,
- "note_sell": 4.9804,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 4.9687,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "sgd",
- "currency_name": "新加坡元",
- "currency_code": "SGD",
- "cash_buy": 5.304,
- "cash_sell": 5.3439,
- "cash_mid": null,
- "note_buy": 5.304,
- "note_sell": 5.3439,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 5.3446,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "thb",
- "currency_name": "泰铢",
- "currency_code": "THB",
- "cash_buy": 0.208,
- "cash_sell": 0.2096,
- "cash_mid": null,
- "note_buy": 0.208,
- "note_sell": 0.2096,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 0.21,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "krw",
- "currency_name": "韩元",
- "currency_code": "KRW",
- "cash_buy": 0.004513,
- "cash_sell": 0.004608,
- "cash_mid": null,
- "note_buy": 0.004513,
- "note_sell": 0.004608,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 0.004554,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "twd",
- "currency_name": "新台币",
- "currency_code": "TWD",
- "cash_buy": null,
- "cash_sell": null,
- "cash_mid": null,
- "note_buy": 0.207,
- "note_sell": 0.2269,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 0.2151,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "chf",
- "currency_name": "瑞士法郎",
- "currency_code": "CHF",
- "cash_buy": 8.6279,
- "cash_sell": 8.6945,
- "cash_mid": null,
- "note_buy": 8.6279,
- "note_sell": 8.6945,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 8.7042,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "sek",
- "currency_name": "瑞典克朗",
- "currency_code": "SEK",
- "cash_buy": 0.7185,
- "cash_sell": 0.7243,
- "cash_mid": null,
- "note_buy": 0.7185,
- "note_sell": 0.7243,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 0.7258,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "dkk",
- "currency_name": "丹麦克朗",
- "currency_code": "DKK",
- "cash_buy": 1.0563,
- "cash_sell": 1.0647,
- "cash_mid": null,
- "note_buy": 1.0563,
- "note_sell": 1.0647,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 1.0643,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "nok",
- "currency_name": "挪威克朗",
- "currency_code": "NOK",
- "cash_buy": 0.7293,
- "cash_sell": 0.7351,
- "cash_mid": null,
- "note_buy": 0.7293,
- "note_sell": 0.7351,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 0.7347,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "myr",
- "currency_name": "林吉特",
- "currency_code": "MYR",
- "cash_buy": 1.7191,
- "cash_sell": 1.7346,
- "cash_mid": null,
- "note_buy": null,
- "note_sell": null,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 1.7358,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "rub",
- "currency_name": "卢布",
- "currency_code": "RUB",
- "cash_buy": 0.0887,
- "cash_sell": 0.0933,
- "cash_mid": null,
- "note_buy": 0.0887,
- "note_sell": 0.0933,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 0.0927,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "nzd",
- "currency_name": "新西兰元",
- "currency_code": "NZD",
- "cash_buy": 3.9665,
- "cash_sell": 3.9963,
- "cash_mid": null,
- "note_buy": 3.9665,
- "note_sell": 3.9963,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 4.0275,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "zar",
- "currency_name": "南非兰特",
- "currency_code": "ZAR",
- "cash_buy": 0.4061,
- "cash_sell": 0.4108,
- "cash_mid": null,
- "note_buy": 0.4061,
- "note_sell": 0.4108,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 0.4139,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "aed",
- "currency_name": "阿联酋迪拉姆",
- "currency_code": "AED",
- "cash_buy": 1.8426,
- "cash_sell": 1.8686,
- "cash_mid": null,
- "note_buy": 1.8426,
- "note_sell": 1.8686,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 1.856,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "brl",
- "currency_name": "巴西雷亚尔",
- "currency_code": "BRL",
- "cash_buy": null,
- "cash_sell": null,
- "cash_mid": null,
- "note_buy": 1.2524,
- "note_sell": 1.3959,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 1.3172,
- "updated_at": "2026-01-27 19:20:33"
-{},
- "currency_slug": "idr",
- "currency_name": "印尼卢比",
- "currency_code": "IDR",
- "cash_buy": 0.000382,
- "cash_sell": 0.000392,
- "cash_mid": null,
- "note_buy": 0.000382,
- "note_sell": 0.000392,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 0.000387,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "inr",
- "currency_name": "印度卢比",
- "currency_code": "INR",
- "cash_buy": null,
- "cash_sell": null,
- "cash_mid": null,
- "note_buy": 0.067327,
- "note_sell": 0.075068,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 0.070901,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "php",
- "currency_name": "菲律宾比索",
- "currency_code": "PHP",
- "cash_buy": 0.1096,
- "cash_sell": 0.1118,
- "cash_mid": null,
- "note_buy": 0.1096,
- "note_sell": 0.1118,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 0.1101,
- "updated_at": "2026-05-17 10:30:00"
-{},
- "currency_slug": "sar",
- "currency_name": "沙特里亚尔",
- "currency_code": "SAR",
- "cash_buy": 1.8036,
- "cash_sell": 1.829,
- "cash_mid": null,
- "note_buy": 1.8036,
- "note_sell": 1.829,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 1.8168,
- "updated_at": "2026-05-17 10:30:00"
-{}
- "currency_slug": "try",
- "currency_name": "土耳其里拉",
- "currency_code": "TRY",
- "cash_buy": 0.1393,
- "cash_sell": 0.1603,
- "cash_mid": null,
- "note_buy": 0.1393,
- "note_sell": 0.1603,
- "note_mid": null,
- "middle_price": null,
- "conversion_price": 0.1499,
- "updated_at": "2026-05-17 10:30:00"
- "engine": "咔咔云外汇牌价智核 v1.0",
- "note": "价格单位均为人民币(CNY)。1单位外币兑换若干人民币。其中部分银行字段为"-"表示该项不公开。"
- "debug": "",
- "exec_time": 0.213,
- "user_ip": "175.161.45.2",
- "timestamp": 1779083754,
- "tip": "此接口由咔咔云独家开发,购买开源接口源码联系QQ:79087446"
| 错误码 | 类型 | 说明 |
|---|---|---|
| 403 | int | 没有权限 |
| 400 | int | 参数传递不正确 |
| 500 | int | 服务器内部错误 |
<?php
/**
* API请求DEMO
*
* 本demo支持常见的HTTP请求方法(GET/POST/PUT/DELETE等)
*/
//基本配置
$api_key = '你的接口密钥,登录控制台后在密钥管理页面申请';
$secret_key = '';
// API请求示例:
try {
$client = new ApiClient($api_key, $secret_key);
$client->setTimeout(10);
$client->setVerifySSL(false); // 关闭SSL验证
// GET请求示例
echo "=== 开始GET请求 ===\n";
$response = $client->get('https://daoyouapi.com/api/aab/index', [
'key' => $api_key,
'key2' => '其他参数'
]);
print_r($response);
//print_r($client->getLastRequestInfo());
/*
// POST表单示例
echo "\n=== 开始POST请求 ===\n";
$response = $client->post('接口地址', [
'key' => $api_key,
'key2' => '其他参数'
]);
print_r($response);
print_r($client->getLastRequestInfo());
// POST JSON示例
echo "\n=== 开始POST JSON请求 ===\n";
$response = $client->postJson('接口地址', [
'key' => $api_key,
'key2' => '其他参数'
]);
print_r($response);
print_r($client->getLastRequestInfo());
*/
} catch (ApiClientException $e) {
echo "API请求错误: " . $e->getMessage();
if ($e->getCode() > 0) {
echo " (HTTP状态码: " . $e->getCode() . ")";
}
print_r($client->getLastRequestInfo() ?? []);
}
/**
* API客户端类
*
* 提供了一个简单的HTTP API客户端实现,支持常见的HTTP请求方法(GET/POST/PUT/DELETE等)
* 具有以下主要功能:
* - 支持 API 密钥和签名认证
* - 可配置请求超时和SSL验证
* - 支持自定义请求头
* - 支持表单和JSON格式的请求体
* - 自动解析响应结果
* - 提供详细的请求信息记录
*
* 使用示例:
* ```
* $client = new ApiClient('https://api.example.com', 'api_key', 'secret_key');
* $response = $client->get('/users', ['page' => 1]);
* ```
*
* @throws ApiClientException 当API请求失败时抛出异常
*/
class ApiClient
{
private $apiKey;
private $secretKey;
private $timeout = 30;
private $verifySSL = true;
private $lastRequestInfo = [];
private $defaultHeaders = [];
/**
* 构造函数
*
* @param string $apiKey API密钥(可选)
* @param string $secretKey 签名密钥(可选)
*/
public function __construct(string $apiKey = '', string $secretKey = '')
{
$this->apiKey = $apiKey;
$this->secretKey = $secretKey;
}
/**
* 设置请求超时时间(秒)
*/
public function setTimeout(int $seconds): self
{
$this->timeout = $seconds;
return $this;
}
/**
* 设置是否验证SSL证书
*/
public function setVerifySSL(bool $verify): self
{
$this->verifySSL = $verify;
return $this;
}
/**
* 添加默认请求头
*/
public function addDefaultHeader(string $name, string $value): self
{
$this->defaultHeaders[$name] = $value;
return $this;
}
/**
* 发送GET请求
*
* @param string $endpoint 接口端点
* @param array $query 查询参数
* @param array $headers 额外请求头
*/
public function get(string $endpoint, array $query = [], array $headers = []): array
{
return $this->request('GET', $endpoint, [
'query' => $query,
'headers' => $headers
]);
}
/**
* 发送POST请求(表单格式)
*
* @param string $endpoint 接口端点
* @param array $data POST数据
* @param array $headers 额外请求头
*/
public function post(string $endpoint, array $data = [], array $headers = []): array
{
return $this->request('POST', $endpoint, [
'form_data' => $data,
'headers' => $headers
]);
}
/**
* 发送POST请求(JSON格式)
*
* @param string $endpoint 接口端点
* @param array $data POST数据
* @param array $headers 额外请求头
*/
public function postJson(string $endpoint, array $data = [], array $headers = []): array
{
return $this->request('POST', $endpoint, [
'json' => $data,
'headers' => array_merge(['Content-Type' => 'application/json'], $headers)
]);
}
/**
* 发送PUT请求
*/
public function put(string $endpoint, array $data = [], array $headers = []): array
{
return $this->request('PUT', $endpoint, [
'json' => $data,
'headers' => $headers
]);
}
/**
* 发送DELETE请求
*/
public function delete(string $endpoint, array $data = [], array $headers = []): array
{
return $this->request('DELETE', $endpoint, [
'json' => $data,
'headers' => $headers
]);
}
/**
* 获取最后一次请求的详细信息
*/
public function getLastRequestInfo(): array
{
return $this->lastRequestInfo;
}
/**
* 基础请求方法
*/
private function request(string $method, string $endpoint, array $options = []): array
{
// 初始化cURL
$ch = curl_init();
$url = ltrim($endpoint, '/');
// 准备请求头
$headers = $this->prepareHeaders($options['headers'] ?? []);
// 处理查询参数
if (!empty($options['query'])) {
$url .= '?' . http_build_query($options['query']);
}
// 处理请求体
$postData = null;
if (isset($options['form_data'])) {
$postData = http_build_query($options['form_data']);
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
} elseif (isset($options['json'])) {
$postData = json_encode($options['json']);
$headers[] = 'Content-Type: application/json';
}
// 设置cURL选项
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_TIMEOUT => $this->timeout,
CURLOPT_SSL_VERIFYPEER => $this->verifySSL,
CURLOPT_SSL_VERIFYHOST => $this->verifySSL,
CURLOPT_HEADER => true,
]);
if ($method !== 'GET' && $postData !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
}
// 执行请求
$response = curl_exec($ch);
$error = curl_error($ch);
$info = $this->lastRequestInfo = curl_getinfo($ch);
curl_close($ch);
// 处理错误
if ($error) {
throw new ApiClientException("cURL请求失败: " . $error);
}
// 分离响应头和响应体
$headerSize = $info['header_size'];
$responseHeaders = substr($response, 0, $headerSize);
$responseBody = substr($response, $headerSize);
// 解析响应
$result = json_decode($responseBody, true) ?? $responseBody;
// 检查HTTP状态码
if ($info['http_code'] >= 400) {
$errorMsg = is_array($result) ? ($result['message'] ?? $responseBody) : $responseBody;
throw new ApiClientException("API请求失败: " . $errorMsg, $info['http_code']);
}
return [
'status' => $info['http_code'],
'headers' => $this->parseHeaders($responseHeaders),
'data' => $result
];
}
/**
* 准备请求头(自动添加签名)
*/
private function prepareHeaders(array $headers): array
{
// 合并默认头
$headers = array_merge($this->defaultHeaders, $headers);
// 添加签名头
if ($this->apiKey && $this->secretKey) {
$timestamp = time();
$signString = "key={$this->apiKey}×tamp={$timestamp}";
$signature = hash_hmac('sha256', $signString, $this->secretKey);
$headers['X-Api-Key'] = $this->apiKey;
$headers['X-Api-Timestamp'] = $timestamp;
$headers['X-Api-Sign'] = $signature;
}
// 转换为cURL格式
$curlHeaders = [];
foreach ($headers as $name => $value) {
$curlHeaders[] = "$name: $value";
}
return $curlHeaders;
}
/**
* 解析响应头
*/
private function parseHeaders(string $headers): array
{
$parsed = [];
foreach (explode("\r\n", $headers) as $i => $line) {
if ($i === 0) {
$parsed['HTTP_CODE'] = $line;
} else {
$parts = explode(': ', $line, 2);
if (count($parts) === 2) {
$parsed[$parts[0]] = $parts[1];
}
}
}
return $parsed;
}
}
class ApiClientException extends \Exception
{
// 自定义异常类
}
//jQuery-Ajax
$.ajax({
url: 'https://daoyouapi.com/api/aab/index',
data: {
//接口参数,一行一个,可按照接口文档-请求参数 的参数填写,或者直接复制开发工具下面的测试代码。
key: '你的接口密钥,登录控制台后在密钥管理页面申请',
参数名: '参数值',
},
type: 'GET', //请求协议(GET或POST),一般默认GET,部分接口需要POST请求,根据实际情况修改为POST即可。
dataType: 'json',
success: function(data) {
console.log(data); //请求成功,输出结果到控制台
},
timeout: 3000, //超时时间
error: function(data) {
console.log('请求失败'); //失败处理
}
});
| 子程序名 | 返回值类型 | 公开 | 备 注 | ||
| __启动窗口_创建完毕 | |||||
| 子程序名 | 返回值类型 | 公开 | 备 注 | ||
| Send_API | |||||
| 变量名 | 类 型 | 静态 | 数组 | 备 注 | ||
| REQU_Data | 文本型 | 提交字符串 | ||||
| return | 文本型 | 返回字符串 | ||||
| API_URL | 文本型 | 接口地址 | ||||
| API_KEY | 文本型 | 接口密钥 | ||||
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Test {
public static void main(String[] args) {
try {
URL url = new URL("https://daoyouapi.com/api/aab/index?key=你的接口密钥,登录控制台后在密钥管理页面申请");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
// 设置请求方式
connection.setRequestMethod("GET");
connection.connect();
// 获取响应码
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
// 读取到的内容给line变量
System.out.println(line);
}
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
// 发起一个GET请求
resp, err := http.Get("https://daoyouapi.com/api/aab/index?key=你的接口密钥,登录控制台后在密钥管理页面申请")
if err != nil {
fmt.Println("http get error", err)
return
}
// 读取响应结果
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("http read error", err)
return
}
// 关闭响应结果
defer resp.Body.Close()
fmt.Println(string(result))
}
```
# 导入requests库
import requests
# 设置url
url = 'https://daoyouapi.com/api/aab/index?key=你的接口密钥,登录控制台后在密钥管理页面申请'
# 发送post请求
response = requests.post(url, data={'key1': 'value1', 'key2': 'value2'})
# 获取响应内容
result = response.json()
# 打印结果
print(result)
```
// 以下是使用Node.js进行GET和POST请求API接口的示例代码:
const https = require('https');
const querystring = require('querystring');
// 定义请求选项
const options = {
hostname: 'daoyouapi.com',
path: '/api/aab/index',
method: 'GET'
};
// 发送GET请求
https.get(options, res => {
console.log(`statusCode: ${res.statusCode}`);
res.on('data', d => {
process.stdout.write(d);
});
}).on('error', error => {
console.error(error);
});
// 发送POST请求
const postData = querystring.stringify({
'key1': 'value1',
'key2': 'value2'
});
const postOptions = {
hostname: 'daoyouapi.com',
path: '/api/aab/index',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
}
};
const postReq = https.request(postOptions, res => {
console.log(`statusCode: ${res.statusCode}`);
res.on('data', d => {
process.stdout.write(d);
});
});
postReq.on('error', error => {
console.error(error);
});
postReq.write(postData);
postReq.end();
/*
这个示例代码使用Node.js内置的`https`模块进行HTTP请求。
首先定义了一个GET请求的选项,然后使用`https.get()`方法发送了GET请求。在响应流上注册回调函数,以便在收到响应数据时将其输出到控制台。在出现错误时,也注册了错误处理程序。
类似地,我们也定义了一个POST请求选项,并使用`https.request()`方法发送它。需要在请求头中包含适当的`Content-Type`和`Content-Length`以确保服务器可以正确解析请求体。请求体由`write()`方法写入,并在请求结束时通过调用`end()`方法通知请求对象已经完成。
注意,此示例默认使用`querystring`模块将数据作为x-www-form-urlencoded格式进行编码。如果需要使用其他格式(如JSON),则需要相应地更改请求头和请求体的编码方式。
另外,为了确保HTTPS请求的安全性,您也可以添加其他选项,例如验证服务器证书、设置代理等。
*/
以下是使用C语言进行GET和POST请求API接口的示例代码:
``` c
#include
#include
#include
#include // 需要安装curl库
// API地址
const char* url = "https://daoyouapi.com/api/aab/index";
// GET请求
void getRequest(CURL* curl) {
CURLcode res;
// 设置URL
curl_easy_setopt(curl, CURLOPT_URL, url);
// 执行请求
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
}
// POST请求
void postRequest(CURL* curl) {
CURLcode res;
// 设置URL
curl_easy_setopt(curl, CURLOPT_URL, url);
// 设置POST数据
const char* postData = "key=你的接口密钥,登录控制台后在密钥管理页面申请&key1=value1";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData);
// 执行请求
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
}
int main() {
CURL* curl;
CURLcode res;
// 初始化curl
curl = curl_easy_init();
if(curl) {
// 设置SSL验证
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
// GET请求
getRequest(curl);
// POST请求
postRequest(curl);
// 清理curl资源
curl_easy_cleanup(curl);
}
return 0;
}
```
这个示例代码使用了libcurl库进行HTTP请求。
首先,需要设置API地址。然后,基于`CURL`结构体创建curl句柄,并使用`curl_easy_setopt()`函数设置选项。这里设置了SSL验证,以确保请求的安全性。
在GET请求中,只需将URL设置为选项,然后调用`curl_easy_perform()`函数执行请求即可。
在POST请求中,还需要将POST数据作为字符串传递给`CURLOPT_POSTFIELDS`选项。
需要注意的是,为了避免内存泄漏,应该在使用完curl句柄之后调用`curl_easy_cleanup()`函数进行清理。
除了上述示例代码外,libcurl库还提供了更多高级选项,例如处理HTTP头、上传文件等。可以参考文档进行更详细的了解。
以下是一个使用C++请求API接口的示例代码:
```cpp
#include
#include
int main() {
CURL *curl;
CURLcode res;
std::string url = "https://daoyouapi.com/api/aab/index?key=你的接口密钥,登录控制台后在密钥管理页面申请";
std::string response;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, [](char *ptr, size_t size, size_t nmemb, void *userdata) -> size_t {
std::string *response = reinterpret_cast(userdata);
response->append(ptr, size * nmemb);
return size * nmemb;
});
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
res = curl_easy_perform(curl);
if (res == CURLE_OK) {
std::cout << "Response: " << response << std::endl;
} else {
std::cerr << "Error: " << curl_easy_strerror(res) << std::endl;
}
curl_easy_cleanup(curl);
}
return 0;
}
```
解释:
1. 引入需要的头文件:``用于输出结果,``用于使用libcurl库。
2. 定义需要请求的API接口的URL和存储响应数据的字符串变量。
3. 初始化一个CURL对象。
4. 设置CURL对象的参数:请求的URL(`CURLOPT_URL`)、是否跟随重定向(`CURLOPT_FOLLOWLOCATION`)、响应数据的写入函数(`CURLOPT_WRITEFUNCTION`)和响应数据的写入位置(`CURLOPT_WRITEDATA`)。
5. 发送HTTP请求并获取响应数据,判断返回状态码是否OK。
6. 清理CURL对象。
运行该程序会发送GET请求到指定的API接口URL,并在程序终止前将响应数据输出到终端。注意,在使用该示例代码之前需要安装libcurl库。
以下是一个使用C#请求API接口的示例代码:
```csharp
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program {
static async Task Main(string[] args) {
HttpClient client = new HttpClient();
string url = "https://daoyouapi.com/api/aab/index?key=你的接口密钥,登录控制台后在密钥管理页面申请";
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode) {
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("Response: " + responseBody);
} else {
Console.WriteLine("Error: " + response.StatusCode);
}
}
}
```
解释:
1. 引用需要的命名空间:`System.Net.Http`用于使用HttpClient类,`System.Threading.Tasks`用于异步执行请求操作。
2. 创建一个HttpClient对象。
3. 定义需要请求的API接口的URL。
4. 发送GET请求到指定的API接口URL,并获取响应结果。
5. 判断响应状态是否成功,如果成功则读取响应数据(使用ReadAsStringAsync方法),否则输出错误信息(使用StatusCode属性)。
运行该程序会发送GET请求到指定的API接口URL,并在程序终止前将响应数据输出到终端。注意,在使用该示例代码之前需要安装.NET Framework或.NET Core SDK。
以下是VB请求API接口的示例代码:
```
' 1. 引入Microsoft XML v6.0库
' 2. 创建一个XMLHTTP对象
Dim xhr As XMLHTTP
Set xhr = New XMLHTTP
' 3. 设置请求的URL、方法,以及是否异步等
xhr.Open "GET", "https://daoyouapi.com/api/aab/index?key=你的接口密钥,登录控制台后在密钥管理页面申请", False
' 4. 发送请求
xhr.send
' 5. 获取响应结果
Dim responseText As String
responseText = xhr.responseText
' 6. 输出响应结果
Debug.Print responseText
' 7. 释放资源
Set xhr = Nothing
```
在这个示例中,我们创建了一个XMLHTTP对象,用于请求API接口。我们先调用`open`方法来设置请求的URL、方法,以及是否异步。然后,我们发送请求,并使用`responseText`属性来获取响应结果。最后,我们将响应结果打印到控制台,完成操作后释放资源,以防止内存泄漏。请注意,以上示例代码为同步请求,如果想使用异步请求,需要设置第三个参数为`True`,并在请求结束时处理`OnReadyStateChange`事件。
| 参数名 | 填写参数值 |
|---|---|
请 登录 后发表评论
填写业务相关参数免费在线调试
生成符合你的开发语言代码,复制即可
调整你后端部分逻辑代码即可上线使用