[PHP开发]使用curlPOST调用AlistAPi查看文件信息

前言

最近博主开了个下载站,每次更新文件都要同步很多页面的更新日期,十分的不银杏,很容易把我们给累死,下载服务是由alist提供的,正好有个API,能不能使用API直接同步更新日期呢?

https://alist.nn.ci/guide/api/fs.html#%E8%BF%94%E5%9B%9E%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-3

查看API文档可以得知,可以通过Post的方式请求Alist服务器:http://Alist服务器.com/api/fs/get

body携带joson

{
  "path": "/t",
  "password": "",
  "page": 1,
  "per_page": 0,
  "refresh": false
}

来获取文件的详细信息

{
  "code": 200,
  "message": "success",
  "data": {
    "name": "root",
    "size": 0,
    "is_dir": true,
    "modified": "0001-01-01T00:00:00Z",
    "sign": "",
    "thumb": "",
    "type": 0,
    "raw_url": "",
    "readme": "",
    "provider": "unknown",
    "related": null
  }
}

返回的json

于是我写了个小程序,代码如下

<?php

// 判断,如果GET的参数非update,就报错
if(isset($_GET['update'])) {
    $update = $_GET['update'];
    // 判断,如果请求的文件不存在,就报错
} else {
    $update = err;
    
}

// 设置请求的URL
$url = 'https://alist服务器.com/api/fs/get';

// 设置请求的body数据
$data = array(
    "path" => "$update",
    "password" => "",
    "page" => 1,
    "per_page" => 0,
    "refresh" => false
);

// 将body数据转换为json格式
$data_json = json_encode($data);

// 创建一个新cURL资源
$ch = curl_init();

// 设置cURL选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_json))
);

// 执行cURL会话
$response = curl_exec($ch);

// 关闭cURL会话
curl_close($ch);

// 解析返回的json数据
$responseArray = json_decode($response, true);

// 检查response是否已成功解码
if ($responseArray && isset($responseArray['data']['modified'])) {
    $modified = $responseArray['data']['modified'];

    // 提取"name"字段的功能
    if(isset($responseArray['data']['name'])) {
        $name = $responseArray['data']['name'];
    } else {
        echo "<h2>未找到文件名字段</h2>";
    }

    // 提取"provider"字段的功能
    if(isset($responseArray['data']['provider'])) {
        $provider = $responseArray['data']['provider'];
    } else {
        echo "<h2>未找到存储类型字段</h2>";
    }
    
       // 提取"type"字段的功能
    if(isset($responseArray['data']['type'])) {
        $type = $responseArray['data']['type'];
    } else {
        echo "<h2>未找到文件类型字段</h2>";
    }
    
    // 提取"size"字段的功能
    if(isset($responseArray['data']['size'])) {
        $size = $responseArray['data']['size'];
        $size_mb = round($size / 1048576, 2);  //Byte转换MB
        echo "<p>最后更新于:$modified </p>";
        echo "<p>文件: $name 存储类型:$provider 类型:$type 大小:$size_mb MB</p>";
    } else {
        echo "<h2>未找到文件大小字段</h2>";
    }
} else {
    echo "<h2>错误发生!!,未找到字段 By:夏的博客</h2>";
}
?>

这时我们请求API文件,并加上参数?update=alist文件路径

可以看到成功返回了文件信息

如果你请求的路径有密码

你需要在// 设置请求的body数据的json数据里面的”password” => “”,数值的””里面填入密码

白名单版

以上的代码可以请求任何的路径,万一我有些文件信息不想让别人看到呢,所以我又做了一个白名单版,只有文件内的路径请求才有效

代码如下

<?php

// 判断,如果GET的参数非update,就报错
if(isset($_GET['update'])) {
    $update = $_GET['update'];
    // 判断,如果请求的文件不存在,就报错
} else {
    $update = err;
    
}

// 读取文件内容
$file = file_get_contents('filelist.txt');

// 按行分割文件内容
$lines = explode("\n", $file);

// 判断参数值是否有效
if($update <= 0 || $update > count($lines)) {
    $link = 1;
} else {
    // 根据参数值获取链接
    $link = $lines[$update - 1];
}

// 设置请求的URL
$url = 'https://Alist服务器.com/api/fs/get';

// 设置请求的body数据
$data = array(
    "path" => "$link",
    "password" => "",
    "page" => 1,
    "per_page" => 0,
    "refresh" => false
);

// 将body数据转换为json格式
$data_json = json_encode($data);

// 创建一个新cURL资源
$ch = curl_init();

// 设置cURL选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_json))
);

// 执行cURL会话
$response = curl_exec($ch);

// 关闭cURL会话
curl_close($ch);

// 解析返回的json数据
$responseArray = json_decode($response, true);

// 检查response是否已成功解码
if ($responseArray && isset($responseArray['data']['modified'])) {
    $modified = $responseArray['data']['modified'];

    // 提取"name"字段的功能
    if(isset($responseArray['data']['name'])) {
        $name = $responseArray['data']['name'];
    } else {
        echo "<h2>未找到文件名字段</h2>";
    }

    // 提取"provider"字段的功能
    if(isset($responseArray['data']['provider'])) {
        $provider = $responseArray['data']['provider'];
    } else {
        echo "<h2>未找到存储类型字段</h2>";
    }
    
       // 提取"type"字段的功能
    if(isset($responseArray['data']['type'])) {
        $type = $responseArray['data']['type'];
    } else {
        echo "<h2>未找到文件类型字段</h2>";
    }
    
    // 提取"size"字段的功能
    if(isset($responseArray['data']['size'])) {
        $size = $responseArray['data']['size'];
        $size_mb = round($size / 1048576, 2);  //Byte转换MB
        echo "<p>最后更新于:$modified </p>";
        echo "<p>文件: $name 存储类型:$provider 类型:$type 大小:$size_mb MB</p>";
    } else {
        echo "<h2>未找到文件大小字段</h2>";
    }
} else {
    echo "<h2>错误发生!!,未找到字段 By:夏的博客</h2>";
}
?>

然后你需要在API文件的文件夹下创建一个文本文件名字为:filelist.txt,然后在里面添加路径,回车键分割

就像这样

/文件夹/blog.php
/wz/wwwroot.zip

这时我们请求API文件,并加上参数?update=filelist.txt文件的路径行数

可以看到成功返回了文件信息

然后你可以通过防火墙屏蔽路径文件的下载,或者说更改路径文件的名称,更改// 读取文件内容那一行’filelist.txt’里面的内容就可以了

网站内显示

你可以iframe嵌入网页,或者写进主题模板文件直接调用函数

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇