Active Collab file upload using PHP SDK

有些话、适合烂在心里 提交于 2019-12-24 17:09:56

问题


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?


回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/36050238/active-collab-file-upload-using-php-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!