farse311
farse311

Reputation: 53

Adding attachments to tasks using php asana library results in empty files in task

I am trying to add attachments to a task using the php asana library (https://github.com/Asana/php-asana). I am successful in 'adding' the files, but when I open them in the task they are completely empty or corrupt.

enter image description here

I have tried .png, .doc, and .pdf all with the same result. My request looks like this:

$attachment = $client->attachments->createOnTask(
  $task->id,
  'screenshot_from_2016-07-08_140457.png',
  'http://edit.local.org/sites/default/files/webform/change-request-materials/screenshot_from_2016-07-08_140457_8.png',
  'image/png'
);

I have also tried using the relative path for the filename /sites/default/files/webform/change-request-materials/screenshot_from_2016-07-08_140457_8.png but get the same result.

This is the 'example code' I used from the library, seems pretty straightforward.

// add an attachment to the task
$demoAttachment = $client->attachments->createOnTask(
   $demoTask->id,
  "hello world",
  "upload.txt",
  "text/plain"
);

Also tried using 2 versions of the the curl request in https://asana.com/developers/api-reference/attachments just to see if I could get the attachments to work at all.

First one:

curl -H "Authorization: Bearer <personal_access_token>" https://app.asana.com/api/1.0/tasks/152938418205845/attachments --form "file=@http://edit.local.org/sites/default/files/webform/change-request-materials/screenshot_from_2016-07-08_140457_12.png;type=image/png"

Resulted in

curl: (26) couldn't open file "http://edit-fca.local.org/sites/default/files/webform/change-request-materials/screenshot_from_2016-07-08_140457_12.png"

And I've 777 on the file and the folder the file is in. So I decided to remove the '@' in front of the file and then got:

{"errors":[{"message":"file: File is not an object","help":"For more information on API status codes and how to handle them, read the docs on errors: https://asana.com/developers/documentation/getting-started/errors"}]}

which when I went to that URL didn't really tell me anything about the File not an object error.

A bit stuck as the php-asana library seems to at least put the files there, but they are empty. While the curl request just seems to not work at all.

Btw I'm using php 5.5.9.

Upvotes: 0

Views: 440

Answers (1)

Mark
Mark

Reputation: 176

I've tried out the example code in the php library and it seems to work fine. I think you may have misunderstood the arguments you are passing to createOnTask. Also, it seems you are passing it the path of a file on the internet you want to upload, but what you can actually do is pass the exact data you want to upload. I'm not familiar enough with php to show you how to get the contents of a file from the internet, but if you have a file locally you can use file_get_contents.

Let's examine the sample code and your code:

$demoAttachment = $client->attachments->createOnTask(
  $demoTask->id,
  "hello world", // Contents of file
  "upload.txt", // The file name of the attachment
  "text/plain" // encoding
);

vs

$attachment = $client->attachments->createOnTask(
  $task->id,
  'screenshot_from_2016-07-08_140457.png', // This should be your image data- but like encoded the right way
  'http://edit.local.org/sites/default/files/webform/change-request-materials/screenshot_from_2016-07-08_140457_8.png', // this should be the filename from above
  'image/png'
);

Heres an example of how you can do this with an image in the same folder.

$demoAttachment = $client->attachments->createOnTask(
  $demoTask->id,
  file_get_contents("someimage.png"), // Contents of file
  "upload.png", // The file name of the attachment
  "image/png" // encoding
);

For reference, see https://asana.com/developers/api-reference/attachments.

Upvotes: 1

Related Questions