对于本人百度网盘有10T资源,长年开通SVIP,有时想分享一些文件,有上万个,一个个手工生成分享链接太费事,然后看了一下,百度网盘有接口,就用动手用ThinkPHP集成百度网盘接口扫描网盘文件自动批量生成分享资源。
先看下效果图:
公用百度网盘API:
namespace baidu;
use baidu\HttpService;
/**
* 开发者:ayumi
* 来源:www.uihtm.com
* 时间:2022-11-15
*/
class BaiduPanApi
{
protected static $config;
public static function config($config)
{
self::$config = $config;
}
/**
* 获取分类文件总个数
* @param $access_token
* @param $path
* @param int $category
* @return bool|string
*/
public static function FileCount($access_token, $path ='/', $category = 6, $recursion = 0)
{
$url = 'https://pan.baidu.com/api/categoryinfo';
$data = [
'access_token' => $access_token,
'parent_path' => $path,
'category' => $category,
'recursion' => $recursion
];
return HttpService::getRequest($url, $data);
}
/**
* 获取文件列表
* @param string $access_token
* @param string $dir
* @param int $start
* @param int $limit
* @return bool|string
*/
public static function XpanFile($access_token = '', $dir = '/', $limit = 1000, $start = 0)
{
$url = 'https://pan.baidu.com/rest/2.0/xpan/file';
$data = [
'method' => 'list',
'access_token' => $access_token,
'dir' => $dir,
'start' => $start,
'limit' => $limit,
];
return HttpService::getRequest($url, $data);
}
/**
* 创建百度分享链接
* @param $appid
* @param $access_token
* @param $fsid_list
* @param int $period
* @return bool|string
*/
public static function share($access_token, $appid, $fsid_list, $period = 0)
{
$url = 'https://pan.baidu.com/apaas/1.0/share/set';
$data = [
'product' => 'netdisk',
'appid' => $appid, //应用ID
'access_token' => $access_token, //鉴权token
'fsid_list' => $fsid_list, //分享文件id列表,json格式,字符串数组
'period' => 0, //分享有效期,单位天, 0:为永久
'pwd' => self::sharePwd(), //分享密码,长度4位,数字+小写字母组成
];
return HttpService::postRequest($url, $data);
}
//生成分享密码
public static function sharePwd($length = 4)
{
$pwd = '';
$chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y','z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
// 在 $chars 中随机取 $length 个数组元素键名
$keys = array_rand($chars, $length);
for($i = 0; $i < $length; $i++)
{
// 将 $length 个数组元素连接成字符串
$pwd .= $chars[$keys[$i]];
}
return $pwd;
}
}