【发布时间】:2023-03-10 21:30:01
【问题描述】:
我正在尝试通过 IPBoards REST Api 上传文件,但不知道如何格式化文件。
这是它现在的样子,但只会出错:
{
"errorCode": "1L296\/B",
"errorMessage": "NO_FILES"
}
代码:
$post = array(
'category' => 1,
'author' => 1,
'title' => 'Test title',
'description' => 'test description',
'files' => "{'test.txt':'".file_get_contents('/home/test/test.txt')."'}",
);
$target_url = 'https://example.com/api/downloads/files';
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 86400);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD,$apikey.":");
$result = curl_exec ($ch);
curl_close ($ch);
这里是 api 文档:API doc
【问题讨论】:
-
尝试:
'files' => json_encode(['test.txt' => json_encode(file_get_contents('/home/test/test.txt'))]),,而不是手动构建 json 字符串。json_encode()将处理任何潜在的逃逸问题。 -
不幸的是不起作用。同样的错误
标签: php rest curl invision-power-board