2016-07-08 21 views
0

我想使用php asana庫(https://github.com/Asana/php-asana)將附件添加到任務。我成功地添加了這些文件,但是當我在任務中打開它們時,它們完全是空的或者已經損壞了。添加附件到使用php asana庫的任務導致任務中的空文件

enter image description here

我曾嘗試巴紐,.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" 
); 

使用2個版本https://asana.com/developers/api-reference/attachments的捲曲請求,只是爲了看看,如果我能得到的附件在所有的工作也試過。

第一個:

curl -H "Authorization: Bearer <personal_access_token>" https://app.asana.com/api/1.0/tasks/152938418205845/attachments --form "[email protected]://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"}]} 

,當我去到該網址沒有真正告訴我有關文件不是什麼OBJ等錯誤。

由於php-asana庫似乎至少會將文件放在那裏,但它們是空的。雖然curl請求似乎根本不起作用。

btw我使用php 5.5.9。

回答

1

我試過了php庫中的示例代碼,它似乎很好地工作。我想你可能誤解了你傳遞給創建任務的論點。另外,看起來你正在將它傳遞到你想要上傳的互聯網上的一個文件的路徑,但你實際上可以做的是傳遞你想要上傳的確切數據。我不太熟悉php,告訴你如何從互聯網上獲取文件的內容,但如果你有本地文件,你可以使用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 
); 

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

繼承人的你如何能做到這在同一文件夾中的圖像的例子。

$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