PHP code example of inetprocess / sugarcrm-apiwrapper

1. Go to this page and download the library: Download inetprocess/sugarcrm-apiwrapper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

inetprocess / sugarcrm-apiwrapper example snippets




netProcess\SugarAPI\BaseRequest;

$url = 'http://127.0.0.1';
$username = 'admin';
$password = 'admin';

$base = new BaseRequest($url);
$base->setUsername($username)->setPassword($password);

// The login can be called dynamically as the class will detect you are not logged
// But to save an API Request, call it manually
$base->login();

// Get the list of Contacts
// '200' is the expected Status Code. If it's not the right one, you'll get an Exception
$data = $base->request('/Contacts', ['OAuth-Token' => $base->getToken()], [], 'get', 200);



netProcess\SugarAPI\SugarClient;

$url = 'http://127.0.0.1';
$username = 'admin';
$password = 'admin';

$client = new SugarClient($url);
$client->setUsername($username)->setPassword($password);


$data = ['first_name' => 'Emmanuel', 'last_name' => 'D.'];
$contact = $client->post('/Contacts', $data);

echo $contact['last_name']; // Should display: "D."


$data = ['first_name' => 'Emmanuel', 'last_name' => 'Dy.'];
$contact = $client->put('/Contacts/' . $contact['id'], $data);

echo $contact['last_name']; // Should display: "Dy"


$contact = $client->get('/Contacts/' . $contact['id']);

echo $contact['last_name']; // Should display: "D."


$contact = $client->delete('/Contacts/' . $contact['id']);


$bulk = $client->newBulkRequest();


$bulk->post('/Contacts', $data);
$bulk->delete('/Contacts/'.$contact['id']);
...
$responses = $bulk->send();



netProcess\SugarAPI\Module;
use InetProcess\SugarAPI\SugarClient;

$url = 'http://127.0.0.1';
$username = 'admin';
$password = 'admin';

$client = new SugarClient($url);
$client->setUsername($username)->setPassword($password);

$module = new Module($client);


$numNotes = $module->search('Notes', [['name' => 'Test']]);

echo "$numNotes in SugarCRM with name = Test";


$notes = $module->search('Notes', [['name' => 'Test']], [], 0, 10, 'name');

if (!empty($notes['records'])) {
    echo $notes['records'][0]['name']; // Displays 'Test'
}


$notes = $module->retrieve('Notes', null, 0, 10);

if (!empty($notes['records'])) {
    echo $notes['records'][0]['name']; // Displays the name of the note
}


$noteId = '123456-abcdef-78910';
$note = $module->retrieve('Notes', $noteId);

if (!empty($note)) {
    echo $note['name']; // Displays the name of the note
}


$data = $module->create('Notes', ['name' => 'Name']);

echo $data['note']; // Displays New Name



$noteId = '123456-abcdef-78910';
$data = $module->update('Notes', $noteId, ['name' => 'New Name']);

echo $data['note']; // Displays New Name



$noteId = '123456-abcdef-78910';
$module->delete('Notes', $noteId);


$contactId = '123456-abcdef-78910';
$casesIds = ['756335-abcdef-12340', '5475626-fedba-545761'];
$res = $module->updateRelatedLinks('Contacts', $contactId, 'cases', $casesIds);
var_export($res);
/*  [
        'linked_records' => ['756335-abcdef-12340', '5475626-fedba-545761'],
        'unlinked_records' => ['234782-gfbeaf-7672'],
        'errors' => [],
    ]
*/


$noteId = '123456-abcdef-78910';
$targetFile = '/tmp/file123456';
$module->download('Notes', $noteId, 'filename', $targetFile);



$noteId = '123456-abcdef-78910';
$localFile = '/tmp/file123456';
$uploadedFile = $module->upload('Notes', $noteId, 'filename', $localFile, 'My File.txt');

echo $uploadedFile['filename']['name']; // Displays "My File.txt"



netProcess\SugarAPI\Dropdown;
use InetProcess\SugarAPI\SugarClient;

$url = 'http://127.0.0.1';
$username = 'admin';
$password = 'admin';

$client = new SugarClient($url);
$client->setUsername($username)->setPassword($password);

$dropdown = new Dropdown($client);
$salesStages = $dropdown->getDropdown('Opportunities', 'sales_stage');