Uploading multiple documents via SugarCRM’s API

sugarcrmdevelopers —  March 9, 2012

Ever wanted to upload documents via SugarCRM’s API but known how to do so? It’s as easy as peas. This example does the following:

– Connects to the SugarCRM API
– Gets a session ID
– Enumerates through a predefined folder full of uploads
– Creates a new note for the first upload
– Attaches a file to that new note
– This process is repeated for all uploads in that folder
– Finally, the session is closed.

Each upload is now actually a ‘Note’ with an attached file. This makes for easier import, easier reporting and allows the same functionality as uploading Documents as each attachment to a note is basically a Document.

Here’s the code


<?php
if(!defined('sugarEntry')) define('sugarEntry', true);

$user_name ='api_username';
$user_password = 'api_password';

if(!empty($user_name)){
$offset = 0;
if(isset($_REQUEST['offset'])){
$offset = $_REQUEST['offset'] + 20;
echo $offset;
}
require_once('sugarpro2/include/nusoap/nusoap.php'); //must also have the nusoap code on the ClientSide.
$soapclient = new nusoapclient('http://mydemoninstance.co.uk/soap.php'); //define the SOAP Client an

echo 'LOGIN:
';
$result = $soapclient->call('login',array('user_auth'=>array('user_name'=>$user_name,'password'=>md5($user_password), 'version'=>'.01'), 'application_name'=>'SoapTest'));
echo 'HERE IS ERRORS:
';
echo $soapclient->error_str;

echo '

HERE IS RESPONSE:
';
echo $soapclient->response;

echo '

HERE IS RESULT:
';
echo print_r($result);
$session = $result['id'];
$user_guid = $soapclient->call('get_user_id',$session_id);
echo($user_guid);
//$user_id = $soapclient->get_user_id($session);

$dir = "/Library/WebServer/Documents/folder/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if (filetype($dir . $file) != "dir"){
echo "filename: $file : filetype: " . filetype($dir . $file) . "
";

$target_path = $dir . $file;

////////////////////// create the note
$params = array(
$session,
'Notes',
array(
array(
'name'=>'name',
'value'=>'Bulk Document2'),
array(
'name'=>'description',
'value'=>'An attachment2'),
)
);
$note = $soapclient->call('set_entry',$params);
echo print_r($note);
echo("NoteID:".$note["id"]);

$contents = file_get_contents ($target_path);

$attachment=array(
'id'=>$note["id"],
'filename'=>$file,
'file'=>base64_encode($contents)
);

$attachmentpp = array(
'session' => $session,
'note' => $attachment
);

echo '

Set Attachment:
';
$result2 = $soapclient->call('set_note_attachment', $attachmentpp);
echo '

HERE IS RESPONSE:
';
echo print_r($result2);

}
}
closedir($dh);
}
}

echo '

LOGOUT:
';
$result = $soapclient->call('logout',array('session'=>$session));
echo 'HERE IS ERRORS:
';
echo $soapclient->error_str;

echo '

HERE IS RESPONSE:
';
echo $soapclient->response;

echo '

HERE IS RESULT:
';
echo print_r($result);

}
?>

How to make it work?

Edit the file to use a username and password which has access to your Sugar instance:

$user_name =’YOUR USERNAME’;
$user_password = ‘YOUR PASSWORD’;

Edit the following line to point to your Sugar instance as I have below – it needs to point to the soap.php file which is how you access the WSDL:

$soapclient = new nusoapclient(‘http://mydemoinstance.co.uk/soap.php&#8217;); //define the SOAP Client

Edit the line below to point to where the files are that you wish to upload:

$dir = “/Library/WebServer/Documents/folder/”;

Upload your files to the web server in the location specified in $dir. Make sure that the php script has permission to read that folder and its contents.

Finally, browse to this file in your browser. Responses from the web service will be presented on screen flowing down the screen.

This is a rough working version which worked fine during my five different tests. Please use this as the basis for any tests or trials you wish to do.

Enjoy!