Upload file via REST API, IPBoard

259 Views Asked by At

Im trying to upload files via IPBoards REST Api but dont really know how to format the files.

This is how it looks right now but only gets error:

{
    "errorCode": "1L296\/B",
    "errorMessage": "NO_FILES"
}

Code:

        $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);

Here is the api documentation: API doc

1

There are 1 best solutions below

0
Patrik BoXon Nicklasson On

Try using the http_build_query method and change the $post array slightly:

'files' => array($filename => $contents),

And change to:

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