0

我试图通过 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

4

1 回答 1

1

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

'文件' => 数组($filename => $contents),

并更改为:

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

于 2017-05-06T16:54:53.790 回答