问题
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