通过vtiger 5.4 webservice添加/上传文件



我试图通过vtiger 5.4 webservice创建一个带有附加文件的文档。

创建文档很简单,但是我不清楚添加文件的过程。这似乎是一个两步的过程:

  1. 上传文件
  2. 将文件引用到文档

除非有一种直接的方法可以将文件与文档对象一起上传,但我找不到任何关于该主题的文档(除了基本的,barebones webservices文档)。

希望有任何指示。谢谢!

您没有指定使用哪种语言,所以我将在这里粘贴一个简单的curl请求,您可以自己尝试一下,并找出如何用您选择的语言复制它。

基本上,您需要做的是将文件作为"multipart/form-data""附加"到为创建新文件而发送的POST请求。

curl -i 
-b vtcookies 
-H "Accept: application/json; charset=UTF-8" 
-X POST 
-F '_operation=saveRecord' 
-F 'module=Documents' 
-F 'session=20a5XXXXXXX9a1ba95c19a' 
-F 'values={"notes_title" : "Example title", "assigned_user_id" : "19x1", "notecontent" : "<p>Some content</p>", "filelocationtype" : "I", "filestatus" : 1, "filename" : "set-your-file-name.png"}' 
-F "file=@"path-to-filename.png";filename="filename.png"" 
http://localhost:8888/vtigercrm540/modules/Mobile/api.php

这个curl示例的实现细节:-b选项告诉curl在文件中查找会话cookie。要使上面的curl请求工作,您必须首先运行以下命令:

curl -i 
-c vtcookies 
-H "Accept: application/json; charset=UTF-8" 
-X POST 
-F '_operation=login' 
-F 'username=your-username' 
-F 'password=your-password' 
http://localhost:8888/vtigercrm530/modules/Mobile/api.php

在明确请求与PHP有关的问题后添加了以下内容。

基于请求的PHP语言,这里有更多信息。我使用了Vtiger WebService浏览器项目,我可以给出更多的提示(该项目是针对Webservices的,而不是针对移动API的,但它提供了一个PHP客户端,其底层概念非常相似)。

Vtiger_HTTP_ClientCurl_HTTP_Client的一个子类。您应该看看send_multipart_post_data,而不是使用send_post_data方法。在HTTP_Client.php文件中有一个名为doPost的方法。我做了一个叫做doPostFile的版本,看起来像这样:

function doPostFile($postdata=false, $file, $decodeResponseJSON=false, $timeout=20) {
    if($postdata === false) $postdata = Array();
    $this->debug = TRUE;
    $resdata = $this->send_multipart_post_data($this->_serviceurl, $postdata, $file, null, $timeout);
    if($resdata && $decodeResponseJSON) $resdata = $this->__jsondecode($resdata);
    return $resdata;
}

在这段代码中,$file变量包含$_FILES["file"],它的var_dump看起来像:

array(6) { ["name"]=> string(28) "@8590567929_72c0ded112_o.jpg" 
["type"]=> string(11) "@image/jpeg" 
["tmp_name"]=> string(36) "/Applications/MAMP/tmp/php/phpZWMXz5" 
["error"]=> int(0) ["size"]=> int(1255732) }

最新更新