在线调试
填写业务相关参数免费在线调试
微博/知乎/百度热榜热搜
免费/测试用户请求频率限制 | 请求频率总限制 | 每日请求次数总限制 |
---|---|---|
1秒10次 每个免费用户的QPS总限制 | 1秒60次 每个用户QPS总限制 | 不限 每个用户每日请求总次数 |
无
接口地址:https://api.beichenwl.cn/api/hotlist
返回格式:application/json
请求方式:HTTPGET
请求示例:https://api.beichenwl.cn/api/hotlist/index?key=你的密钥
名称 | 必填 | 类型 | 示例值 | 说明 |
---|---|---|---|---|
key | 是 | string | 35kj5jnlj53453kl5j43nj5 | 接口密钥,在控制台->密钥管理查看 |
type | 是 | string | weibo、zhihu、baidu (填写其中一种热点标识) | 热点标识 |
名称 | 类型 | 说明 |
---|---|---|
success | int | 状态码 |
title | string | 热点标识名称 |
subtitle | string | 标识名称副标题 |
update_time | string | 热点更新时间 |
data | string | 请求结果数据集 |
{
"success": true,
"title": "微博",
"subtitle": "热搜榜",
"update_time": "2023-04-18 10:10:22",
-
"data": [],
"index": 52,
"title": "不要低估川渝地区的微辣",
"hot": "98452万",
"url": "https://s.weibo.com/weibo?q=#不要低估川渝地区的微辣#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#不要低估川渝地区的微辣#&t=31&band_rank=12&Refer=top"
"index": 51,
"title": "彩虹瀑布 忘不掉的奇景",
"hot": "99480万",
"url": "https://s.weibo.com/weibo?q=彩虹瀑布 忘不掉的奇景&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=彩虹瀑布 忘不掉的奇景&t=31&band_rank=12&Refer=top"
"index": 50,
"title": "年轻人游古城的正确打开方式",
"hot": "99941万",
"url": "https://s.weibo.com/weibo?q=#年轻人游古城的正确打开方式#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#年轻人游古城的正确打开方式#&t=31&band_rank=12&Refer=top"
"index": 49,
"title": "马斯克回应星舰首飞推迟",
"hot": "101897万",
"url": "https://s.weibo.com/weibo?q=#马斯克回应星舰首飞推迟#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#马斯克回应星舰首飞推迟#&t=31&band_rank=12&Refer=top"
"index": 48,
"title": "男生花钱现状",
"hot": "116475万",
"url": "https://s.weibo.com/weibo?q=#男生花钱现状#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#男生花钱现状#&t=31&band_rank=12&Refer=top"
"index": 47,
"title": "gidle回归时间",
"hot": "117568万",
"url": "https://s.weibo.com/weibo?q=gidle回归时间&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=gidle回归时间&t=31&band_rank=12&Refer=top"
"index": 46,
"title": "是什么让你犹豫进入婚姻",
"hot": "120679万",
"url": "https://s.weibo.com/weibo?q=#是什么让你犹豫进入婚姻#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#是什么让你犹豫进入婚姻#&t=31&band_rank=12&Refer=top"
"index": 45,
"title": "aespa新专概念预告视频",
"hot": "123575万",
"url": "https://s.weibo.com/weibo?q=#aespa新专概念预告视频#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#aespa新专概念预告视频#&t=31&band_rank=12&Refer=top"
"index": 44,
"title": "鹿晗又染粉发",
"hot": "141367万",
"url": "https://s.weibo.com/weibo?q=#鹿晗又染粉发#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#鹿晗又染粉发#&t=31&band_rank=12&Refer=top"
"index": 43,
"title": "大熊猫暖暖 被打",
"hot": "145480万",
"url": "https://s.weibo.com/weibo?q=大熊猫暖暖 被打&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=大熊猫暖暖 被打&t=31&band_rank=12&Refer=top"
"index": 42,
"title": "成年人要学会在职场勇敢说不",
"hot": "146335万",
"url": "https://s.weibo.com/weibo?q=#成年人要学会在职场勇敢说不#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#成年人要学会在职场勇敢说不#&t=31&band_rank=12&Refer=top"
"index": 41,
"title": "上海车展",
"hot": "150717万",
"url": "https://s.weibo.com/weibo?q=#上海车展#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#上海车展#&t=31&band_rank=12&Refer=top"
"index": 40,
"title": "谷爱凌科切拉随拍",
"hot": "154880万",
"url": "https://s.weibo.com/weibo?q=#谷爱凌科切拉随拍#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#谷爱凌科切拉随拍#&t=31&band_rank=12&Refer=top"
"index": 39,
"title": "黑莲花 绝世大饼",
"hot": "157351万",
"url": "https://s.weibo.com/weibo?q=黑莲花 绝世大饼&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=黑莲花 绝世大饼&t=31&band_rank=12&Refer=top"
"index": 38,
"title": "kpop念经曲",
"hot": "158391万",
"url": "https://s.weibo.com/weibo?q=kpop念经曲&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=kpop念经曲&t=31&band_rank=12&Refer=top"
"index": 37,
"title": "虞书欣丁禹兮 黑莲花",
"hot": "173240万",
"url": "https://s.weibo.com/weibo?q=虞书欣丁禹兮 黑莲花&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=虞书欣丁禹兮 黑莲花&t=31&band_rank=12&Refer=top"
"index": 36,
"title": "特斯拉回应载孕妇轿车方向盘突然抱死",
"hot": "182163万",
"url": "https://s.weibo.com/weibo?q=#特斯拉回应载孕妇轿车方向盘突然抱死#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#特斯拉回应载孕妇轿车方向盘突然抱死#&t=31&band_rank=12&Refer=top"
"index": 35,
"title": "姜涛曾参加快乐男声",
"hot": "182674万",
"url": "https://s.weibo.com/weibo?q=#姜涛曾参加快乐男声#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#姜涛曾参加快乐男声#&t=31&band_rank=12&Refer=top"
"index": 34,
"title": "理想AD Max",
"hot": "188568万",
"url": "https://s.weibo.com/weibo?q=理想AD Max&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=理想AD Max&t=31&band_rank=12&Refer=top"
"index": 33,
"title": "老师出门散步顺手救了个人",
"hot": "194385万",
"url": "https://s.weibo.com/weibo?q=#老师出门散步顺手救了个人#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#老师出门散步顺手救了个人#&t=31&band_rank=12&Refer=top"
"index": 32,
"title": "黄渤新片学爸定档",
"hot": "195558万",
"url": "https://s.weibo.com/weibo?q=#黄渤新片学爸定档#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#黄渤新片学爸定档#&t=31&band_rank=12&Refer=top"
"index": 31,
"title": "厦门暴雨",
"hot": "197962万",
"url": "https://s.weibo.com/weibo?q=厦门暴雨&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=厦门暴雨&t=31&band_rank=12&Refer=top"
"index": 30,
"title": "长空之王开预售",
"hot": "198461万",
"url": "https://s.weibo.com/weibo?q=#长空之王开预售#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#长空之王开预售#&t=31&band_rank=12&Refer=top"
"index": 29,
"title": "NBA季后赛",
"hot": "205961万",
"url": "https://s.weibo.com/weibo?q=NBA季后赛&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=NBA季后赛&t=31&band_rank=12&Refer=top"
"index": 28,
"title": "SMTOWN遭扣押搜查",
"hot": "210530万",
"url": "https://s.weibo.com/weibo?q=#SMTOWN遭扣押搜查#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#SMTOWN遭扣押搜查#&t=31&band_rank=12&Refer=top"
"index": 27,
"title": "长期戴耳机",
"hot": "236592万",
"url": "https://s.weibo.com/weibo?q=#长期戴耳机#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#长期戴耳机#&t=31&band_rank=12&Refer=top"
"index": 26,
"title": "腿粗是因为走路姿势不对",
"hot": "247357万",
"url": "https://s.weibo.com/weibo?q=#腿粗是因为走路姿势不对#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#腿粗是因为走路姿势不对#&t=31&band_rank=12&Refer=top"
"index": 25,
"title": "从奖学金到助学金",
"hot": "251523万",
"url": "https://s.weibo.com/weibo?q=#从奖学金到助学金#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#从奖学金到助学金#&t=31&band_rank=12&Refer=top"
"index": 24,
"title": "特斯拉又在美国发生致命车祸",
"hot": "252176万",
"url": "https://s.weibo.com/weibo?q=#特斯拉又在美国发生致命车祸#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#特斯拉又在美国发生致命车祸#&t=31&band_rank=12&Refer=top"
"index": 23,
"title": "黄牛你不要太离谱",
"hot": "258412万",
"url": "https://s.weibo.com/weibo?q=#黄牛你不要太离谱#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#黄牛你不要太离谱#&t=31&band_rank=12&Refer=top"
"index": 22,
"title": "山东人均存款8.37万",
"hot": "289815万",
"url": "https://s.weibo.com/weibo?q=#山东人均存款8.37万#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#山东人均存款8.37万#&t=31&band_rank=12&Refer=top"
"index": 21,
"title": "张含韵 没想到30岁会这么好",
"hot": "299184万",
"url": "https://s.weibo.com/weibo?q=张含韵 没想到30岁会这么好&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=张含韵 没想到30岁会这么好&t=31&band_rank=12&Refer=top"
"index": 20,
"title": "长月烬明发布理性追剧声明",
"hot": "321036万",
"url": "https://s.weibo.com/weibo?q=#长月烬明发布理性追剧声明#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#长月烬明发布理性追剧声明#&t=31&band_rank=12&Refer=top"
"index": 19,
"title": "苹果版余额宝上线",
"hot": "331135万",
"url": "https://s.weibo.com/weibo?q=#苹果版余额宝上线#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#苹果版余额宝上线#&t=31&band_rank=12&Refer=top"
"index": 18,
"title": "长沙暴雨",
"hot": "351654万",
"url": "https://s.weibo.com/weibo?q=长沙暴雨&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=长沙暴雨&t=31&band_rank=12&Refer=top"
"index": 17,
"title": "总要去一次黄河三角洲吧",
"hot": "366919万",
"url": "https://s.weibo.com/weibo?q=#总要去一次黄河三角洲吧#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#总要去一次黄河三角洲吧#&t=31&band_rank=12&Refer=top"
"index": 16,
"title": "哪些人属于胃癌高危人群",
"hot": "388537万",
"url": "https://s.weibo.com/weibo?q=#哪些人属于胃癌高危人群#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#哪些人属于胃癌高危人群#&t=31&band_rank=12&Refer=top"
"index": 15,
"title": "杨幂聚餐",
"hot": "404984万",
"url": "https://s.weibo.com/weibo?q=杨幂聚餐&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=杨幂聚餐&t=31&band_rank=12&Refer=top"
"index": 14,
"title": "周杰伦向网易索赔205万元",
"hot": "414830万",
"url": "https://s.weibo.com/weibo?q=#周杰伦向网易索赔205万元#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#周杰伦向网易索赔205万元#&t=31&band_rank=12&Refer=top"
"index": 13,
"title": "刘雯好薄",
"hot": "422680万",
"url": "https://s.weibo.com/weibo?q=#刘雯好薄#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#刘雯好薄#&t=31&band_rank=12&Refer=top"
"index": 12,
"title": "要离职了被同事要求请吃饭",
"hot": "432797万",
"url": "https://s.weibo.com/weibo?q=#要离职了被同事要求请吃饭#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#要离职了被同事要求请吃饭#&t=31&band_rank=12&Refer=top"
"index": 11,
"title": "臀部发力的走路姿势我真的悟了",
"hot": "553165万",
"url": "https://s.weibo.com/weibo?q=#臀部发力的走路姿势我真的悟了#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#臀部发力的走路姿势我真的悟了#&t=31&band_rank=12&Refer=top"
"index": 10,
"title": "2023考研报录比约6比1",
"hot": "593558万",
"url": "https://s.weibo.com/weibo?q=#2023考研报录比约6比1#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#2023考研报录比约6比1#&t=31&band_rank=12&Refer=top"
"index": 9,
"title": "新生儿落户被标注文盲",
"hot": "613185万",
"url": "https://s.weibo.com/weibo?q=#新生儿落户被标注文盲#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#新生儿落户被标注文盲#&t=31&band_rank=12&Refer=top"
"index": 8,
"title": "国内首款共创6x6皮卡",
"hot": "685909万",
"url": "https://s.weibo.com/weibo?q=&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=&t=31&band_rank=12&Refer=top"
"index": 7,
"title": "宋茜说分手还可以做朋友",
"hot": "778465万",
"url": "https://s.weibo.com/weibo?q=#宋茜说分手还可以做朋友#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#宋茜说分手还可以做朋友#&t=31&band_rank=12&Refer=top"
"index": 6,
"title": "数名外籍女子晒中国游引出偷渡案",
"hot": "865479万",
"url": "https://s.weibo.com/weibo?q=#数名外籍女子晒中国游引出偷渡案#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#数名外籍女子晒中国游引出偷渡案#&t=31&band_rank=12&Refer=top"
"index": 5,
"title": "女生称陪嫁300万男友更急着结婚",
"hot": "882160万",
"url": "https://s.weibo.com/weibo?q=#女生称陪嫁300万男友更急着结婚#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#女生称陪嫁300万男友更急着结婚#&t=31&band_rank=12&Refer=top"
"index": 4,
"title": "小米13Ultra发布",
"hot": "882781万",
"url": "https://s.weibo.com/weibo?q=&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=&t=31&band_rank=12&Refer=top"
"index": 3,
"title": "这些旅游专列卷起来了",
"hot": "891027万",
"url": "https://s.weibo.com/weibo?q=#这些旅游专列卷起来了#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#这些旅游专列卷起来了#&t=31&band_rank=12&Refer=top"
"index": 2,
"title": "左肩有你 振兴钢厂",
"hot": "933930万",
"url": "https://s.weibo.com/weibo?q=左肩有你 振兴钢厂&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=左肩有你 振兴钢厂&t=31&band_rank=12&Refer=top"
"index": 1,
"title": "感觉新冠不再来了还用打疫苗吗",
"hot": "1309430万",
"url": "https://s.weibo.com/weibo?q=#感觉新冠不再来了还用打疫苗吗#&t=31&band_rank=12&Refer=top",
"mobilUrl": "https://s.weibo.com/weibo?q=#感觉新冠不再来了还用打疫苗吗#&t=31&band_rank=12&Refer=top"
"copyright": "北辰聚合API api.beichenwl.cn"
}
错误码 | 类型 | 说明 |
---|---|---|
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://api.beichenwl.cn/api/hotlist', [
'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://api.beichenwl.cn/api/hotlist',
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://api.beichenwl.cn/api/hotlist?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://api.beichenwl.cn/api/hotlist?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://api.beichenwl.cn/api/hotlist?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: 'api.beichenwl.cn',
path: '/api/hotlist/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: 'api.beichenwl.cn',
path: '/api/hotlist/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://api.beichenwl.cn/api/hotlist";
// 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://api.beichenwl.cn/api/hotlist?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://api.beichenwl.cn/api/hotlist?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://api.beichenwl.cn/api/hotlist?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`事件。
参数名 | 填写参数值 |
---|---|
请 登录 后发表评论
填写业务相关参数免费在线调试
生成符合你的开发语言代码,复制即可
调整你后端部分逻辑代码即可上线使用