1

I just started working with the API using this documentation, and I'm having trouble uploading files. I get no response from the api call to upload-files. I've tried with multiple file types with no success. Here is my API call:

$client = new \ActiveCollab\SDK\Client($token);
try {
    $response = $client->post('upload-files',[
        [
            'test-file.txt',
            'text\/plain'
        ]
    ]);

    echo $response->getBody();
} catch(AppException $e) {
    print $e->getMessage() . '<br><br>';
}

According to the documentation I should receive a response containing a file upload code, but I receive no response, not even a Validation Error. It doesn't throw an exception either. I've had no problems with any other requests so I don't think its an authentication issue. Can anyone please help me figure out what I'm doing wrong?

Ilija
  • 4,105
  • 4
  • 32
  • 46
Christian
  • 41
  • 1
  • 8

2 Answers2

1

In order to upload files, you should pass an array of file paths or path - MIME type pairs as third argument of client's post method:

$response = $client->post('upload-files', [], ['/path/to/file.png']);
$response = $client->post('upload-files', [], ['/path/to/file.png' => 'image/png']);

This works only if files on the given path (/path/to/file.png in this example) exist and are readable.

Ilija
  • 4,105
  • 4
  • 32
  • 46
  • Thanks for your response! I'm still having some trouble though, when I try the first way `$response = $client->post('upload-files', [], ['images/test-image']);` I get the exception `ErrorException in Client.php line 222: File 'images/test-image' is not readable`. When I try the second way `$response = $client->post('upload-files', [], ['images/test-image.jpg' => 'image/jpeg']);` I get the response `ErrorException in Client.php line 222: File 'image/jpeg' is not readable`, any idea what I'm doing wrong? I think the file is fine, it's used throughout our application with no issue. – Christian Mar 18 '16 at 16:15
  • You are not providing a valid path to a file. I recommend that you use absolute path of a file on the sile system: `/tmp/file-that-i-want-to-upload.jpg` or `c:\MyFolder\MyFile.jpg`. – Ilija Mar 18 '16 at 16:21
  • I've updated the answer to make it clear that files that you are trying to upload need to exist on the file system. – Ilija Mar 18 '16 at 16:23
1

Had the same problem and here's how I solved it:

From inside the post method in the SDK:

if (is_array($file)) {
    list($path, $mime_type) = $file;
}

from php.net:

In PHP 5, list() assigns the values starting with the right-most parameter.
In PHP 7, list() starts with the left-most parameter.

I'm using php 5.6 so I swapped:

['/path/to/file.png' => 'image/png']

to:

['image/png' => '/path/to/file.png']

Works as intended now.