【问题标题】:Upload file via REST API, IPBoard通过 REST API、IPBoard 上传文件
【发布时间】: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


【解决方案1】:

尝试使用 http_build_query 方法并稍微更改 $post 数组:

'files' => 数组($filename => $contents),

然后改为:

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));

【讨论】:

    猜你喜欢
    • 2021-06-07
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 2018-08-06
    • 1970-01-01
    相关资源
    最近更新 更多