php-trello-api
php-trello-api copied to clipboard
Issue with attachments.
Can you show me please how can an attachment can be added?
Here is my current code:
$reply_upload = $client->api('cards')->attachments()->create($id_card, array( 'file' => DOCROOT.'media/uploads/issue_tracker/'.$options['filename'], 'mimeType' => File::mime_by_ext($ext), 'name' => uniqid().'.'.$ext ));
The issue is that if I put the path to the file on the file parameter .. it shows in the content of the Trello attachment but if I use file_get_contents on it, it is ok there but the mime is not set at all.
Thanks!
$client = new Client();
$client->setHttpClient(new HttpClient());
$client->authenticate(self::API_KEY, self::TOKEN, Client::AUTH_URL_CLIENT_ID);
$client->cards()->attachments()->create($id, ['file' => $name, 'name' => basename($name)]);
and
class HttpClient extends \Trello\HttpClient\HttpClient
{
protected function createRequest($httpMethod, $path, $body = null, array $headers = array(), array $options = array())
{
$path = $this->options['api_version'].'/'.$path;
if ($httpMethod === 'GET' && $body) {
$path .= (false === strpos($path, '?') ? '?' : '&');
$path .= utf8_encode(http_build_query($body, '', '&'));
}
$request = $this->client->createRequest(
$httpMethod,
$path,
array_merge($this->headers, $headers),
$body,
$options
);
if ( isset($body['file']) )
{
$request = $request->addPostFiles(array('file' => $body["file"]));
}
return $request;
}
}
Thank you @xsen !
For those who are using using Laravel Trello, this also functions with the facade 😄 .
Trello::setHttpClient(new YourHttpClient());
Trello::authenticate(config('trello.api_key'), config('trello.api_token'), \Trello\Client::AUTH_URL_CLIENT_ID);
Trello::card()
->attachments()
->create('card_id_here', ['file' => $name, 'name' => basename($name)]);
What is $name? We are trying to do this as well following Trello's docs but after the upload, Trello will only have the *.tmp files
@molerat619 $name is file full path