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

团队合作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"

我正在使用ZF1表单获取数据,并使用cURL将它们发送到Teamwork API。 我尝试创建新任务 在任务列表上,没关系。 问题是,当我尝试附加文件时,服务器响应是: p>

“MESSAGE”:“找不到临时文件参考”,“状态”:“错误 “ p> blockquote>

我该怎么办? p>

请帮忙。 p>

我的表格 : p>

  $ this-> setName('editForm')
  - > setAttrib('class','no-store')
  - > setAttrib(  'enctype','multipart / form-data''); 
 
 $ this-> createElement('file','pendingFileAttachments')
  code>  pre> 
 
 

我只是从$ _POST'pendingFileAttachments'获取并尝试在json中编码并发送。可能需要获取文件而不仅仅是文件名,这是真的吗? p>

提前感谢帮助 编辑: p>

编辑: p>

所以,我发现我应该先将文件上传到Teamwork API,然后在创建任务中使用上传的文件参考 POST cURL函数。 p>

现在我已将文件上传到服务器的硬盘上,在我的php脚本中,我使用“realpath”来查找真正存在且感觉良好的文件。 p>

比我使用此代码: p>

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

并获得响应: p>

“MESSAGE”:“表单字段'文件'不包含有效文件”,“状态 “:”错误“ p> blockquote> div>

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.