api.php
POST
上传图片到微博图床,支持本地图片上传和远程图片URL上传
参数名 | 必选 | 类型 | 说明 |
---|---|---|---|
sub | 是 | string | 微博SUB值,不包含"SUB="前缀 |
image | 二选一 | file | 本地图片文件 |
url | 二选一 | string | 远程图片URL地址 |
返回JSON格式数据
{ "code": 200, // 状态码,200表示成功,非200表示失败 "pid": "xxxxxxxx", // 微博图片ID "size": 12345, // 图片大小(字节) "width": 800, // 图片宽度 "height": 600, // 图片高度 "url": "https://wx1.sinaimg.cn/large/xxxxxxxx.jpg" // 图片URL }
$url = 'http://您的域名/api.php'; $sub = '您的微博SUB值'; $file = '/path/to/image.jpg'; $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => [ 'sub' => $sub, 'image' => new CURLFile($file) ] ]); $result = curl_exec($ch); curl_close($ch); echo $result;
$url = 'http://您的域名/api.php'; $sub = '您的微博SUB值'; $imageUrl = 'http://example.com/image.jpg'; $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => [ 'sub' => $sub, 'url' => $imageUrl ] ]); $result = curl_exec($ch); curl_close($ch); echo $result;
// 上传本地图片 async function uploadLocalImage(file, sub) { const formData = new FormData(); formData.append('image', file); formData.append('sub', sub); const response = await fetch('api.php', { method: 'POST', body: formData }); return await response.json(); } // 上传远程图片 async function uploadRemoteImage(imageUrl, sub) { const formData = new FormData(); formData.append('url', imageUrl); formData.append('sub', sub); const response = await fetch('api.php', { method: 'POST', body: formData }); return await response.json(); }