0

我正在尝试使用 php asana 库(https://github.com/Asana/php-asana)向任务添加附件。我成功地“添加”了文件,但是当我在任务中打开它们时,它们完全为空或损坏。

在此处输入图像描述

我试过 .png、.doc 和 .pdf 都得到相同的结果。我的请求如下所示:

$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'
);

我也尝试过使用文件名的相对路径,/sites/default/files/webform/change-request-materials/screenshot_from_2016-07-08_140457_8.png但得到了相同的结果。

这是我从库中使用的“示例代码”,看起来很简单。

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

还尝试在https://asana.com/developers/api-reference/attachments中使用 2 个版本的 curl 请求,以查看是否可以让附件正常工作。

第一:

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"

导致

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"

我在文件和文件所在的文件夹上有 777。所以我决定删除文件前面的“@”,然后得到:

{"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"}]}

当我访问那个 URL 时,它并没有真正告诉我关于文件不是对象错误的任何信息。

有点卡住,因为 php-asana 库似乎至少将文件放在那里,但它们是空的。虽然 curl 请求似乎根本不起作用。

顺便说一句,我使用的是 php 5.5.9。

4

1 回答 1

1

我已经尝试了 php 库中的示例代码,它似乎工作正常。我认为您可能误解了您传递给 createOnTask 的参数。此外,您似乎正在将要上传的 Internet 上的文件的路径传递给它,但您实际上可以做的是传递您要上传的确切数据。我对 php 不够熟悉,无法向您展示如何从 Internet 获取文件的内容,但如果您在本地有文件,则可以使用 file_get_contents。

让我们检查示例代码和您的代码:

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

对比

$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'
);

下面是一个示例,说明如何使用同一文件夹中的图像执行此操作。

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

有关参考,请参阅https://asana.com/developers/api-reference/attachments

于 2016-07-12T06:14:42.660 回答