Skip to content

Instantly share code, notes, and snippets.

@ashfame
Created December 12, 2011 15:44
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ashfame/1467944 to your computer and use it in GitHub Desktop.
Save ashfame/1467944 to your computer and use it in GitHub Desktop.
SugarCRM REST API call examples
<?php
/**
* SugarCRM RESTS API Call examples
*
* REST API Wrapper - https://github.com/asakusuma/SugarCRM-REST-API-Wrapper-Class
*/
?>
<!doctype html>
<head>
<meta charset="utf-8">
<title>SugarCRM REST API Wrapper Class Example</title>
</head>
<body>
<pre>
<?php
if ( !file_exists( 'sugar_rest.php' ) ) {
echo 'You need to put the REST API wrapper in the same directory. Get it from here - <a href="https://github.com/asakusuma/SugarCRM-REST-API-Wrapper-Class">SugarCRM REST API Wrapper</a>';
die();
}
require_once( 'sugar_rest.php' );
$sugar = new Sugar_REST();
$error = $sugar->get_error();
if ( $error !== FALSE ) {
echo $error['name'];
}
/**
* Get a list of Accounts in SugarCRM
*/
$results = $sugar->get_with_related(
'Accounts',
array(
'Accounts' => array( 'id', 'name' ),
//'contacts' => array( 'id', 'first_name', 'last_name', 'primary_address_city', 'primary_address_state', 'primary_address_street' )
),
array( 'limit' => '999' )
);
// NOTE: $results['entry_list'] is an array containing all the accounts
echo '<p>There are '.count( $results['entry_list'] ).' accounts</p>';
echo '<ol>';
foreach ( $results['entry_list'] as $entry )
echo "<li>{$entry['name_value_list']['name']['value']} ({$entry['id']})</li>"; // Format: NAME (ID)
echo '</ol>';
/**
* Get a list of Contacts under a particular Account in SugarCRM
*
* $results['relationship_list'] of API call to retreieve Accounts list doesn't return all the contacts under accounts (limited to 50), so we will refetch contacts by specifying the account ID
*/
$results = $sugar->get_with_related(
'Accounts',
array(
'Accounts' => array( 'id' ),
'contacts' => array( 'id', 'first_name', 'last_name', 'primary_address_city', 'primary_address_state', 'primary_address_street' )
),
//array("limit" => "999"),
array("where" => "accounts.id = 'd135a3a1-d637-b383-dbbd-4e03ece62580'") // ID for a particular account, here it is of Hanson
);
$records = $results['relationship_list'][0][0]['records'];
echo '<p>There are '.count( $records ).' contacts under this account</p>';
echo '<ol>';
foreach( $records as $record ) {
echo '<li>';
echo '<ul>';
foreach( $record as $field )
echo "<li>{$field['name']} === {$field['value']}</li>";
echo '</ul>';
echo '</li>';
}
echo '</ol>';
@theboardsaredown
Copy link

Thank you for publishing this! It helped me a lot. I'm struggling on how to search for a user by email though. Do you have any idea how to do this?

@MakarandMane
Copy link

I want to insert leads using API calls to sugarcrm. Can you share any simple url to me, where I will get an proper idea. Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment