通过REST API,IPBoard上传文件

时间:2017-05-06 14:05:04

标签: php rest curl invision-power-board

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

1 个答案:

答案 0 :(得分:1)

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

  

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

改为:

  

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