<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上传到远程接口</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 20px auto;
padding: 20px;
}
.upload-form {
background: #f5f5f5;
padding: 20px;
border-radius: 5px;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
input[type="file"] {
padding: 5px;
}
input[type="submit"] {
background: #4CAF50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background: #45a049;
}
.message {
margin-top: 20px;
padding: 10px;
border-radius: 4px;
}
.success {
background-color: #dff0d8;
color: #3c763d;
}
.error {
background-color: #f2dede;
color: #a94442;
}
.response {
margin-top: 20px;
padding: 15px;
background: #f8f9fa;
border-radius: 4px;
white-space: pre-wrap;
font-family: monospace;
}
</style>
</head>
<body>
<h1>文件上传到远程接口</h1>
<div class="upload-form">
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="fileToUpload">选择要上传的文件:</label>
<input type="file" name="image" id="fileToUpload" required>
</div>
<input type="submit" value="上传到远程接口" name="submit">
</form>
</div>
<?php
if (isset($_POST["submit"])) {
// 检查文件是否上传成功
if ($_FILES['image']['error'] !== UPLOAD_ERR_OK) {
$errorMessages = [
UPLOAD_ERR_INI_SIZE => '文件超过PHP配置的最大限制',
UPLOAD_ERR_FORM_SIZE => '文件超过表单指定的最大限制',
UPLOAD_ERR_PARTIAL => '文件仅部分上传',
UPLOAD_ERR_NO_FILE => '未选择文件',
UPLOAD_ERR_NO_TMP_DIR => '缺少临时文件夹',
UPLOAD_ERR_CANT_WRITE => '文件写入失败',
UPLOAD_ERR_EXTENSION => '文件上传被扩展阻止'
];
$error = $errorMessages[$_FILES['image']['error']] ?? '未知错误';
echo '<div class="message error">文件上传失败: ' . $error . '</div>';
exit;
}
// 配置信息
$token = '8113a06fd1284c7ec'; // 内测阶段,需要联系我们单独获取
$url = 'https://tuku.fuyx.cn/api'; // 远程API地址
// 准备表单数据
$data = [
'filename' => $_FILES['image']['name'],
'md5' => md5_file($_FILES['image']['tmp_name']),
'size' => $_FILES['image']['size'],
'file' => new CURLFile($_FILES['image']['tmp_name'], $_FILES['image']['type'], $_FILES['image']['name'])
];
// 初始化CURL
$ch = curl_init();
// 设置CURL选项
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $token
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置超时时间
// 执行请求
$response = curl_exec($ch);
$curlError = curl_error($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// 处理响应
if ($response === false) {
echo '<div class="message error">CURL请求失败: ' . htmlspecialchars($curlError) . '</div>';
} else {
echo '<div class="message success">请求已发送,HTTP状态码: ' . $httpCode . '</div>';
echo '<div class="response">接口响应: ' . $response . '</div>';
}
}
?>
</body>
</html>