doulao1934
doulao1934
2015-09-28 09:20

团队合作api,如何发送文件?

已采纳

I'm using ZF1 form to take data and cURL to send them to Teamwork API. I try create new task on task list and it's OK. Problem is, when I try to attach the file, the server response is:

"MESSAGE":"Temp file reference not found","STATUS":"Error"

What should I do?

Please help.

My form:

$this->setName('editForm')
     ->setAttrib('class', 'no-store')
     ->setAttrib('enctype', 'multipart/form-data"');

$this->createElement('file', 'pendingFileAttachments')

And I just get from $_POST 'pendingFileAttachments' and try to encode in json and sent. Probably is need to get file not just file name, is this true?

Thank from help in advance.

EDIT:

So, I've found that I should first upload the file to Teamwork API and after use uploaded file ref in create task POST cURL function.

Now I have uploaded file on server's hard disk and in my php script i use "realpath" to finde that is really exist and feel well.

Than I use this code:

$data["file"] = $filePath;

$channel = curl_init();
curl_setopt($channel, CURLOPT_URL, "$this->baseURL/pendingfiles.json");
curl_setopt($channel, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($channel, CURLOPT_POST, true);
curl_setopt($channel, CURLOPT_POSTFIELDS, $data);
curl_setopt($channel, CURLOPT_HTTPHEADER, array("Authorization: BASIC " . base64_encode($this->api_key . ":xxx"),
));
curl_setopt($channel, CURLOPT_SSL_VERIFYPEER, FALSE);
$response = curl_exec($channel);

and get response:

"MESSAGE":"The form field 'file' did not contain a valid file","STATUS":"Error"

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douguizhuang8276 douguizhuang8276 6年前

    The problem was in ZendFramework. Properly way to get file path after upload in ZF1 form is:

    $file = $form->getElement('pendingFileAttachments')->getTransferAdapter()->getFileInfo();
    $file['tmp_name'] // this is filepath
    

    That's all.

    点赞 评论 复制链接分享

为你推荐