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