2

I'm trying to create a document with an attached filed via the vtiger 5.4 webservice.

Creating a document is straight-forward enough, but I'm not clear on the procedure to add the file. It seems like a two step process:

  1. Upload the file
  2. Reference the file to the document

Unless there's a direct way to upload the file alongside the document object, but I can't find any sort of documentation on the subject (other than the basic, barebones webservices docs).

Hoping for any pointers. Thanks!

maggix
  • 3,268
  • 1
  • 22
  • 36
sdragnev
  • 468
  • 3
  • 10

1 Answers1

2

you did not specify which language you are using, so I will paste here a simple curl request that you can try out yourself and figure out how to replicate it in the language of your choice.

Basically, what you need to do is to "attach" the file as "multipart/form-data" to the POST request you are sending for creating the new file.

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

Implementation detail for this curl sample: the -b options tells curl to look for the session cookie into a file. To make the above curl request work you have to first run the following one:

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

Added the following after specifically requesting the question to be concerned with PHP.

Based on the requested PHP language, here are more info. I played around with the Vtiger WebService Browser project and I can give a couple of more hints (the project is for the Webservices and not for the Mobile API, but offers a PHP client to play with and the underlying concepts are very similar).

The Vtiger_HTTP_Client is a subclass of Curl_HTTP_Client. Instead of using the send_post_data method, you should have a look at send_multipart_post_data. In the HTTP_Client.php file there is a method called doPost. I made a version of it called doPostFile that looks like this:

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;
}

In this code, the $file variable contains $_FILES["file"] and its var_dump looks like:

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) }
maggix
  • 3,268
  • 1
  • 22
  • 36