PHP代理webdav 123云盘示例
2025-11-01原创插件祥技术支持107°c
A+ A-php代理webdav 123云盘示例
<?php
/*
* @Author : @祥 技术支持
* @Mail : shjiz@qq.com
* @Date : 2025-12-17 10:39:20
* @LastEditTime : 2025-12-17 14:32:00
* @LastEditors : @祥💥 技术支持
* @Description :
* @FilePath : \webdav\index.php
* It's up to you ^_^
* Copyright (c) 2025 by shjiz@qq.com, All Rights Reserved.
*/
error_reporting(0);
// 设置脚本默认字符集
header('Content-Type: text/html; charset=UTF-8');
// 确保PHP使用UTF-8处理字符串
define('DEFAULT_CHARSET', 'UTF-8');
// 配置WebDAV连接信息
$webdavUrl = 'https://webdav.123pan.cn/webdav';
$username = '1*********2';
$password = '*******************************f****';
$REQUEST_URI = $_SERVER['REQUEST_URI'];
$REQUEST_URI = preg_replace('/\?.+$/', '', $REQUEST_URI);
// 直接使用原始URL,由getinfo函数负责正确编码处理
$remoteFilePath = urldecode($REQUEST_URI);
$encoding = mb_detect_encoding($remoteFilePath, ['UTF-8', 'GBK', 'GB2312'], true);
if ($encoding !== 'UTF-8') {
$remoteFilePath = mb_convert_encoding($remoteFilePath, 'UTF-8', $encoding);
}
$getinfo = getinfo($webdavUrl, $username, $password, $remoteFilePath);
//print_r($getinfo);exit();
if($getinfo['http_code'] != 302){
header('HTTP/1.1 '. $getinfo['http_code']);
}else{
if(!$getinfo['redirect_url'])
{
header('HTTP/1.1 404 Not Found');
}else{
new_header($remoteFilePath);
$url = $getinfo['redirect_url'];
echo echo_file_content($url);
}
}
function getinfo($webdavUrl, $username, $password, $remoteFilePath)
{
$ch = curl_init();
// 先解码,再正确编码,确保URL格式正确
$decodedPath = urldecode($remoteFilePath);
// 将路径分割成 segments
$pathSegments = explode('/', $decodedPath);
$encodedSegments = array();
// 对每个 segment 进行重新编码
foreach ($pathSegments as $segment) {
if (!empty($segment)) {
$encodedSegments[] = rawurlencode($segment);
} else {
$encodedSegments[] = $segment;
}
}
$encodedPath = implode('/', $encodedSegments);
$fullUrl = $webdavUrl . $encodedPath;
curl_setopt($ch, CURLOPT_URL, $fullUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $username. ':'. $password);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$response = curl_exec($ch);
$getinfo = curl_getinfo($ch);
return $getinfo;
}
function echo_file_content($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
'Cache-Control: no-cache',
'Connection: keep-alive',
'Pragma: no-cache',
'Sec-Fetch-Dest: document',
'Sec-Fetch-Mode: navigate',
'Sec-Fetch-Site: none',
'Sec-Fetch-User: ?1',
'Upgrade-Insecure-Requests: 1',
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0',
'sec-ch-ua: "Microsoft Edge";v="143", "Chromium";v="143", "Not A(Brand";v="24"',
'sec-ch-ua-mobile: ?0',
'sec-ch-ua-platform: "Windows"',
]);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
function new_header($url){
ob_end_clean();
$url_array = explode('.', $url);
$name = $url_array[count($url_array)-1];
header('Content-Disposition: inline');
switch ($name) {
case 'jpg':
header('Content-Type: image/jpeg');
break;
case 'jpeg':
header('Content-Type: image/jpeg');
break;
case 'png':
header('Content-Type: image/png');
break;
case 'gif':
header('Content-Type: image/jpeg');
break;
default:
header('Content-Disposition: attachment');
header('Content-Type: application/octet-stream');
break;
}
}123云盘分享
更新时间:2025年12月17日 14:37:08



