Alist图床 PHP代理访问示例 套个CDN似乎就很6B
这个操作属于脱了裤子放气,不过也有一定的用处...
测试环境,西部数码的虚拟主机,感觉良好~
这是一个PHP文件
<?php error_reporting(0); header("Access-Control-Allow-Origin: *"); $alist_url = 'http://127.0.0.1:5244'; $alist_user = 'balabala'; $alist_pass = 'meiyoumima'; $max_size = 50; // M 超过此大小的文件 直接跳转链接 $path = $_SERVER['REQUEST_URI']; if(count(explode('/',$path)) < 3){ http_response_code(403); exit(); // 根目录不允许访问! } $rs = getAlistFiles(urldecode($path),''); if($rs['code'] == 200){ if($rs['data']['size'] > $max_size * 1024 * 1024){ header('Location: ' . $rs['data']['raw_url']); }else{ Content($rs['data']['raw_url']); } }else{ http_response_code($rs['code']); } function Content($Location){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $Location); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate'); curl_setopt($ch, CURLOPT_TIMEOUT, 60); $headers = array(); $headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.289 Safari/537.36'; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); $header = curl_getinfo($ch); curl_close($ch); $header['content_type'] == 'application/oct-stream' ? ContentType() : header("Content-Type: " . $header['content_type']); header("Content-length: " . $header['size_download']); echo $result; } // 没有返回类型的 自己添加 function ContentType(){ $ContentType = array( // video 'mp4' => 'video/mp4', // audio 'mp3' => 'audio/mpeg', // text 'css' => 'text/css', 'js' => 'text/javascript', // image 'png' => 'image/png', 'gif' => 'image/gif', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpg', 'bmp' => 'image/bmp', // application // multipart // message ); $array = explode('.', explode('?',$_SERVER['REQUEST_URI'])['0']); $name = $array[count($array)-1]; if(isset($ContentType[$name])){ header('Content-Type: ' . $ContentType[$name]); }else{ header('Content-Type: application/octet-stream'); } } function getAlistToken() { $cacheName = 'alist_' . $GLOBALS['alist_user'] . '_token'; if (is_file($cacheName)) { $cache = json_decode(file_get_contents($cacheName),1); if($cache['expires'] > time()) { return $cache['token']; } } $Url = $GLOBALS['alist_url'] . '/api/auth/login'; $Body = '{"username": "' . $GLOBALS['alist_user'] . '","password": "' . $GLOBALS['alist_pass'] . '"}'; $Header = array("Content-Type:application/json;charset=utf-8;", "Accept:application/json", "charset=utf-8"); $Data = json_decode(request($Url, 'POST', $Header, $Body), 1); if ($Data['code'] == 200) { $Data['data']['expires'] = time() + 7200; file_put_contents($cacheName, json_encode($Data['data'])); return $Data['data']['token']; } else { exit('ERROR:' . $Data['message']); } } function getAlistFiles($path, $password = '', $page = '', $per_page = '', $refresh = false) { $Url = $GLOBALS['alist_url'] . '/api/fs/get'; $Header = array( 'Authorization:' . getAlistToken(), 'Content-Type: application/json;charset=UTF-8', ); $Body = array( 'path' => $path, 'password' => $password, 'page' => $page, 'per_page' => $per_page, 'refresh' => $refresh, ); $Data = json_decode(request($Url, 'POST', $Header, json_encode($Body)), 1); return $Data; } function request($Url, $Method = "GET", $Header = '', $Data = '') { $Ch = curl_init(); curl_setopt($Ch, CURLOPT_URL, $Url); curl_setopt($Ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($Ch, CURLOPT_SSL_VERIFYHOST, FALSE); if ($Method == "GET") { curl_setopt($Ch, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($Ch, CURLOPT_POST, false); curl_setopt($Ch, CURLOPT_POSTFIELDS, $Data); } if ($Method == "POST") { curl_setopt($Ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($Ch, CURLOPT_POST, true); curl_setopt($Ch, CURLOPT_POSTFIELDS, $Data); } curl_setopt($Ch, CURLOPT_TIMEOUT, 10); curl_setopt($Ch, CURLOPT_HTTPHEADER, $Header); curl_setopt($Ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($Ch, CURLOPT_ENCODING, 'gzip'); $Output = curl_exec($Ch); curl_close($Ch); return $Output; } ?>
可能需要个伪静态
<IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] </IfModule>
可能还需要个跨域
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" /> <add name="Access-Control-Allow-Headers" value="Content-Type" /> </customHeaders> </httpProtocol> </system.webServer> </configuration