【问题标题】:Adding attachments to tasks using php asana library results in empty files in task使用 php asana 库向任务添加附件会导致任务中出现空文件
【发布时间】:2016-07-08 14:28:37
【问题描述】:

我正在尝试使用 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。

【问题讨论】:

    标签: php curl attachment asana asana-api


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      • 1970-01-01
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多