php-trello-api icon indicating copy to clipboard operation
php-trello-api copied to clipboard

Issue with attachments.

Open ghost opened this issue 10 years ago • 4 comments

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!

ghost avatar Aug 18 '15 10:08 ghost

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

}

xsen avatar Oct 11 '16 20:10 xsen

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

danrichards avatar Feb 03 '17 02:02 danrichards

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 avatar Jun 28 '18 08:06 molerat619

@molerat619 $name is file full path

xsen avatar Aug 01 '18 03:08 xsen