在线调试
填写业务相关参数免费在线调试
我是超级APIkey项目通用接口地址( https://apikey.net/api/ )无需注册登录、无需获取key即可调用我噢!
免费/测试用户请求频率限制 | 请求频率总限制 | 每日请求次数总限制 |
---|---|---|
1秒10次 每个免费用户的QPS总限制 | 1秒10次 每个用户QPS总限制 | 不限 每个用户每日请求总次数 |
接口地址:https://apikey.net/api/index
返回格式:application/json
请求方式:HTTPGETPOST/JSON
请求示例:https://apikey.net/api/index?key=你的密钥
名称 | 值 |
---|---|
Content-Type | application/x-www-form-urlencoded;charset:utf-8; |
名称 | 必填 | 类型 | 示例值 | 说明 |
---|---|---|---|---|
key | 是 | string | ehFayFSEyjDrxTFHaSxrDFkCtnjbts5D | 接口密钥,在控制台->密钥管理查看 |
ip | 否 | string | https://apikey.net/api/index?ip=baidu.com | (请求该接口参数,会返回域名解析的服务器IP地址、归属地等信息) |
ip | 否 | string | https://apikey.net/api/index?ip=5.76.86.56 | (请求该接口参数,会返回IP地址的归属地等信息) |
ip | 否 | string | https://apikey.net/api/index?ip=88888888 | (请求该接口参数,会返回数字IP地址的数字IP地址及归属地等信息) |
saying | 否 | string | https://apikey.net/api/index?type=saying | (请求该接口参数,会返回随机的谚语、诗句、散文、经典名句等) |
greeting | 否 | string | https://apikey.net/api/index?type=greeting | (请求该接口参数,会返回北京时间问候语) |
ipAddress | 否 | string | https://apikey.net/api/index?type=ipAddress | (请求该接口参数,会返回用户的IP地址) |
ipLong | 否 | string | https://apikey.net/api/index?type=ipLong | (请求该接口参数,会返回用户IP地址的数字IP地址) |
ipLocation | 否 | string | https://apikey.net/api/index?type=ipLocation | (请求该接口参数,会返回用户IP地址的归属地) |
ipLocation2 | 否 | string | https://apikey.net/api/index?type=ipLocation2 | (请求该接口参数,会返回用户IP地址归属地的国际化) |
ipCountry | 否 | string | https://apikey.net/api/index?type=ipCountry | (请求该接口参数,会返回用户IP地址归属地的国家) |
ipRegion | 否 | string | https://apikey.net/api/index?type=ipRegion | (请求该接口参数,会返回用户IP地址归属地的省份、州域) |
ipCity | 否 | string | https://apikey.net/api/index?type=ipCity | (请求该接口参数,会返回用户IP地址归属地的城市) |
ipLatitude | 否 | string | https://apikey.net/api/index?type=ipLatitude | (请求该接口参数,会返回用户IP地址经纬度中的纬度) |
ipLongitude | 否 | string | https://apikey.net/api/index?type=ipLongitude | (请求该接口参数,会返回用户IP地址经纬度中的经度) |
UserAgent | 否 | string | https://apikey.net/api/index?type=UserAgent | (请求该接口参数,会返回用户浏览器的信息) |
dailyImg | 否 | string | https://apikey.net/api/index?type=dailyImg | (请求该接口参数,会返回微软公司的每日一图的图片地址) |
hImg | 否 | string | https://apikey.net/api/index?type=hImg | (请求该接口参数,会返回竖版格式的图片地址) |
wImg | 否 | string | https://apikey.net/api/index?type=wImg | (请求该接口参数,会返回横版格式的图片地址) |
mImg | 否 | string | https://apikey.net/api/index?type=mImg | (请求该接口参数,会返回美女格式的图片地址) |
bgImg | 否 | string | https://apikey.net/api/index?type=bgImg | (请求该接口参数,会返回适合做背景图的图片地址) |
bgAudio | 否 | string | https://apikey.net/api/index?type=bgAudio | (请求该接口参数,会返回适合做背景声音的音频地址) |
serverTime | 否 | string | https://apikey.net/api/index?type=serverTime | (请求该接口参数,会返回标准版的年月日时分秒的北京时间) |
timestamp | 否 | string | https://apikey.net/api/index?type=timestamp | (请求该接口参数,会返回北京时间的:时间戳格式) |
week | 否 | string | https://apikey.net/api/index?type=week | (请求该接口参数,会返回北京时间的:星期格式) |
timeHour | 否 | string | https://apikey.net/api/index?type=timeHour | (请求该接口参数,会返回北京时间的:小时格式) |
currenThour | 否 | string | https://apikey.net/api/index?type=currenThour | (请求该接口参数,会返回北京时间的:人性化小时格式) |
goodTime | 否 | string | https://apikey.net/api/index?type=goodTime | (请求该接口参数,会返回北京时间的:问候语) |
currentDate | 否 | string | https://apikey.net/api/index?type=currentDate | (请求该接口参数,会返回北京时间的:基本格式 (Basic Format)年月日) |
currentdate | 否 | string | https://apikey.net/api/index?type=currentdate | (请求该接口参数,会返回北京时间的:扩展格式 (Extended Format)年月日) |
CurrentDate | 否 | string | https://apikey.net/api/index?type=CurrentDate | (请求该接口参数,会返回北京时间的:长格式 (Long Format)年月日) |
dayTime | 否 | string | https://apikey.net/api/index?type=dayTime | (请求该接口参数,会返回北京时间的:纯数字年月日格式) |
hourTime | 否 | string | https://apikey.net/api/index?type=hourTime | (请求该接口参数,会返回北京时间的:纯数字年月日小时格式) |
minuteTime | 否 | string | https://apikey.net/api/index?type=minuteTime | (请求该接口参数,会返回北京时间的:纯数字年月日小时分钟格式) |
currenttime | 否 | string | https://apikey.net/api/index?type=currenttime | (请求该接口参数,会返回北京时间的:纯数字年月日小时分秒格式) |
Currenttime | 否 | string | https://apikey.net/api/index?type=Currenttime | (请求该接口参数,会返回北京时间的:扩展格式 (Extended Format)遵循 ISO 8601 标准) |
CurrentTime | 否 | string | https://apikey.net/api/index?type=CurrentTime | (请求该接口参数,会返回北京时间的:国际化标准和完整的 ISO 8601 日期时间格式) |
currentTime | 否 | string | https://apikey.net/api/index?type=currentTime | (请求该接口参数,会返回北京时间的:人性化年月日时分秒显示) |
md5LowTimestamp | 否 | string | https://apikey.net/api/index?type=md5LowTimestamp | (请求该接口参数,北京时间时间戳的MD5小写格式加密) |
md5Timestamp | 否 | string | https://apikey.net/api/index?type=md5Timestamp | (请求该接口参数,北京时间时间戳的MD5大写格式加密) |
md5CurrentDate | 否 | string | https://apikey.net/api/index?type=md5CurrentDate | (请求该接口参数,北京时间年月日的MD5格式加密) |
md5CurrentTime | 否 | string | https://apikey.net/api/index?type=md5CurrentTime | (请求该接口参数,北京时间年月日时分秒的MD5格式加密) |
pw | 否 | string | https://apikey.net/api/index?type=pw | (请求该接口参数,会返回一个8~16字符之间高复杂度密码字符串) |
pw2 | 否 | string | https://apikey.net/api/index?type=pw2 | (请求该接口参数,会返回一个2字符的高复杂度密码字符串) |
pw4 | 否 | string | https://apikey.net/api/index?type=pw4 | (请求该接口参数,会返回一个4字符的高复杂度密码字符串) |
pw6 | 否 | string | https://apikey.net/api/index?type=pw6 | (请求该接口参数,会返回一个6字符的高复杂度密码字符串) |
pw8 | 否 | string | https://apikey.net/api/index?type=pw8 | (请求该接口参数,会返回一个8字符的高复杂度密码字符串) |
pw10 | 否 | string | https://apikey.net/api/index?type=pw10 | (请求该接口参数,会返回一个10字符的高复杂度密码字符串) |
pw12 | 否 | string | https://apikey.net/api/index?type=pw12 | (请求该接口参数,会返回一个12字符的高复杂度密码字符串) |
pw14 | 否 | string | https://apikey.net/api/index?type=pw14 | (请求该接口参数,会返回一个14字符的高复杂度密码字符串) |
pw16 | 否 | string | https://apikey.net/api/index?type=pw16 | (请求该接口参数,会返回一个16字符的高复杂度密码字符串) |
pw18 | 否 | string | https://apikey.net/api/index?type=pw18 | (请求该接口参数,会返回一个18字符的高复杂度密码字符串) |
pw20 | 否 | string | https://apikey.net/api/index?type=pw20 | (请求该接口参数,会返回一个20字符的高复杂度密码字符串) |
pw24 | 否 | string | https://apikey.net/api/index?type=pw24 | (请求该接口参数,会返回一个24字符的高复杂度密码字符串) |
pw26 | 否 | string | https://apikey.net/api/index?type=pw26 | (请求该接口参数,会返回一个26字符的高复杂度密码字符串) |
pw28 | 否 | string | https://apikey.net/api/index?type=pw28 | (请求该接口参数,会返回一个28字符的高复杂度密码字符串) |
pw30 | 否 | string | https://apikey.net/api/index?type=pw30 | (请求该接口参数,会返回一个30字符的高复杂度密码字符串) |
pw32 | 否 | string | https://apikey.net/api/index?type=pw32 | (请求该接口参数,会返回一个32字符的高复杂度密码字符串) |
pw34 | 否 | string | https://apikey.net/api/index?type=pw34 | (请求该接口参数,会返回一个34字符的高复杂度密码字符串) |
pw36 | 否 | string | https://apikey.net/api/index?type=pw36 | (请求该接口参数,会返回一个36字符的高复杂度密码字符串) |
pw38 | 否 | string | https://apikey.net/api/index?type=pw38 | (请求该接口参数,会返回一个38字符的高复杂度密码字符串) |
pw50 | 否 | string | https://apikey.net/api/index?type=pw50 | (请求该接口参数,会返回一个50字符的高复杂度密码字符串) |
pw99 | 否 | string | https://apikey.net/api/index?type=pw99 | (请求该接口参数,会返回一个99字符的高复杂度密码字符串) |
pw100 | 否 | string | https://apikey.net/api/index?type=pw100 | (请求该接口参数,会返回一个100字符的高复杂度密码字符串) |
pw128 | 否 | string | https://apikey.net/api/index?type=pw128 | (请求该接口参数,会返回一个128字符的高复杂度密码字符串) |
pw256 | 否 | string | https://apikey.net/api/index?type=pw256 | (请求该接口参数,会返回一个256字符的高复杂度密码字符串) |
pw512 | 否 | string | https://apikey.net/api/index?type=pw512 | (请求该接口参数,会返回一个512字符的高复杂度密码字符串) |
pw1024 | 否 | string | https://apikey.net/api/index?type=pw1024 | (请求该接口参数,会返回一个1024字符的高复杂度密码字符串) |
pw1~10000任意数 | 否 | string | https://apikey.net/api/index?type=pw1~10000 | (请求该接口参数,会返回一个1~10000字符的高复杂度密码字符串) |
json | 否 | string | https://apikey.net/api/index?type=json | (请求该接口参数,会返回API接口的JSON格式数据) |
script | 否 | string | https://apikey.net/api/index?type=script | (请求该接口参数,会返回API接口的SCRIPT格式数据) |
jsonp | 否 | string | https://apikey.net/api/index?type=jsonp | (请求该接口参数,会返回API接口的JSONP格式数据) |
xml | 否 | string | https://apikey.net/api/index?type=xml | (请求该接口参数,会返回API接口的XML格式数据) |
Hublink | 否 | string | https://apikey.net/api/index?type=Hublink | (请求该接口参数,会返回API接口的JS格式数据) |
index | 否 | string | https://apikey.net/api/index?type=index | (请求该接口参数,会返回定制化的浏览器主页界面) |
mp4 | 否 | string | https://apikey.net/api/index?type=mp4 | (请求该接口参数,会返回MP4视频内容) |
audio | 否 | string | https://apikey.net/api/index?type=audio | (请求该接口参数,会返回Audio音频内容) |
music | 否 | string | https://apikey.net/api/index?type=music | (请求该接口参数,会返回Music音频内容) |
timeDiff | 否 | string | https://apikey.net/api/index?type=timeDiff | (请求该接口参数,会获得指定时间到北京时间的间隔人性化标识) |
jstimeDiff | 否 | string | https://apikey.net/api/index?type=jstimeDiff | (请求该接口参数,会获得指定时间到北京时间的间隔人性化JS标识) |
否 | string | https://apikey.net/api/index?type= | (请求该接口不带参数,会返回API接口的JSON格式数据) | |
否 | string | https://apikey.net/api/ | (请求该接口不带参数,会返回API接口的JSON格式数据) |
名称 | 类型 | 说明 |
---|---|---|
code | int | 状态码 |
msg | string | 状态信息 |
data | string | 请求结果数据集 |
debug | string/array | 调试数据 |
exec_time | float | 执行耗时 |
user_ip | string | 客户端IP |
{
"code": 200,
"msg": "❥ 超级API项目已提供安全、稳定、免费、敏捷服务6年9个月10天7小时23分钟4秒。(接口文档请参考:https://apikey.net/doc/7#doc)",
"data": {
"saying": "❥ 前不见古人,后不见来者、念天地之悠悠,独怆然而剃下。",
"greeting": "晚间好,温馨家庭时光!",
"ipAddress": "183.28.81.12",
"ipLong": 3072086284,
"ipLocation": "广东省佛山市 电信 ",
"ipLocation2": "China,Guangdong,Foshan",
"ipCountry": "China",
"ipRegion": "Guangdong",
"ipCity": "Foshan",
"ipLatitude": 23.0242,
"ipLongitude": 113.1334,
"UserAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36",
"dailyImg": "https://www.bing.com/th?id=OHR.PointReyesSeashore_ZH-CN0076789582_1920x1080.jpg&rf=LaDigue_1920x1080.jpg",
"hImg": "https://SSL.APIkey.Net/Himg/3tXTNA5itNrAmXY8Et7ccLLdMjLepLmC.jpg!APIkey.Net",
"wImg": "https://SSL.APIkey.Net/Wimg/As8kpzw7iGfZKWeKj6WSctFWcSGNmQR2.jpg!APIkey.Net",
"mImg": "https://SSL.APIkey.Net/Mimg/3RFtrdAt6miCJf2mHJbcTpE8s3EasBxh.jpg!APIkey.Net",
"bgImg": "https://SSL.APIkey.Net/Mimg/3RFtrdAt6miCJf2mHJbcTpE8s3EasBxh.jpg!APIkey.Net",
"bgAudio": "https://SSL.APIkey.Net/Audio/60S/cPjitHcPSaHhYcG6PpfXbBKFwDBFyyYQ.mp3",
"serverTime": "2025年09月13日 21:02:12",
"timestamp": 1757768532,
"week": "星期六",
"timeHour": "21",
"currenThour": "现在是晚上9点!",
"goodTime": "晚上好!",
"currentDate": "20250913",
"currentdate": "2025-09-13",
"CurrentDate": "2025年09月13日",
"dayTime": "20250913",
"hourTime": "2025091321",
"minuteTime": "202509132102",
"currenttime": "20250913210212",
"Currenttime": "2025-09-13 21:02:12",
"CurrentTime": "2025-09-13T21:02:12+08:00",
"currentTime": "2025年09月13日21时02分12秒",
"md5LowTimestamp": "1c44ce5ed430301d8d11bc0618700f79",
"md5Timestamp": "1C44CE5ED430301D8D11BC0618700F79",
"md5CurrentDate": "0F7B5DAA22F6B3DA2064519106ED49FE",
"md5CurrentTime": "24DA63B7EA637C0934FD0D352DFE6365",
"pw": "xlULDNLxZmmpcrE",
"pw2": "Xk",
"pw4": "hUNn",
"pw6": "EuLGY7",
"pw8": "3d7QMHHn",
"pw10": "axmC9bzZTg",
"pw12": "xbwqdDH9iIWB",
"pw14": "3XfJ38czkPizjp",
"pw16": "g9BCUQjK4JECMUfL",
"pw18": "goxiyVhK5G5UTdSTU3",
"pw20": "cp3cHmvRfSc7q9976V6d",
"pw24": "Xr8Mk8KEvUcvTpXTV77h8gpT",
"pw26": "GQBjSJEn9CMYC475LVFoaWNhdo",
"pw28": "z9DYXj6CL3NuPClqdRiru3muJfbA",
"pw30": "e5rUnzhMVFIg4PoChGMvPjhPF3Hv1u",
"pw32": "LId1iou8twFYnk3WMbseegrky1PiUTK3",
"pw34": "v8Si8C8Qp7WcNqB4tcQaQ5T2qcMm50dJbd",
"pw36": "LNfZhN74jWJTcCzFobIvyAHavWsKeUtjijrp",
"pw38": "nfyh7rD9pV5eykZNXmKu8bQgTK5lVAoIJf3nyJ",
"pw50": "GrgFlwfXzpTHN2lIlBCIBQjCIMCr35JnOybxbSnU1Jhgizg01k",
"pw99": "JPSsfFDNTiOGCiZNu4D98eJwQBMLVwjTBFx9W32CWKvSOV9LDjhxQlVCoRcTgx6twF3fnlGgbQHBAOm1oqqohBwqUS6U9QwEUie",
"pw100": "BelEt00P8X24JF4TCl8HJceGJh5Z9KRwWEnWzrCxl3mVEZFmzX0AqRk1ZaoocxEOEjnDAegsdTkS9j6p1ZAOh6iSPq8zAfo8L28K",
},
"exec_time": 0.205483,
"ip": "183.28.81.12"
}
<?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://apikey.net/api/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://apikey.net/api/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://apikey.net/api/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://apikey.net/api/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://apikey.net/api/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: 'apikey.net',
path: '/api/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: 'apikey.net',
path: '/api/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://apikey.net/api/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://apikey.net/api/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://apikey.net/api/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://apikey.net/api/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`事件。
参数名 | 填写参数值 |
---|---|
请 登录 后发表评论
填写业务相关参数免费在线调试
生成符合你的开发语言代码,复制即可
调整你后端部分逻辑代码即可上线使用