Retrieving a List of Records - REST and PHP
Retrieving a List of Records - REST and PHP
Overview
A PHP example demonstrating how to retrieve a list of records from a module with the get_entry_list method using cURL and the v4 REST API.
This example will retrieve a list of leads.
Example
<?php
$url = "http://{site_url}/service/v4/rest.php";
$username = "admin";
$password = "password";
//function to make cURL request
function call($method, $parameters, $url)
{
ob_start();
$curl_request = curl_init();
curl_setopt($curl_request, CURLOPT_URL, $url);
curl_setopt($curl_request, CURLOPT_POST, 1);
curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl_request, CURLOPT_HEADER, 1);
curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
$jsonEncodedData = json_encode($parameters);
$post = array(
"method" => $method,
"input_type" => "JSON",
"response_type" => "JSON",
"rest_data" => $jsonEncodedData
);
curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($curl_request);
curl_close($curl_request);
$result = explode("\r\n\r\n", $result, 2);
$response = json_decode($result[1]);
ob_end_flush();
return $response;
}
//login --------------------------------------------
$login_parameters = array(
"user_auth"=>array(
"user_name"=>$username,
"password"=>md5($password),
"version"=>"1"
),
"application_name"=>"RestTest",
"name_value_list"=>array(),
);
$login_result = call("login", $login_parameters, $url);
/*
echo "<pre>";
print_r($login_result);
echo "</pre>";
*/
//get session id
$session_id = $login_result->id;
//get list of records --------------------------------
$get_entry_list_parameters = array(
//session id
'session' => $session_id,
//The name of the module from which to retrieve records
'module_name' => 'Leads',
//The SQL WHERE clause without the word "where".
'query' => "",
//The SQL ORDER BY clause without the phrase "order by".
'order_by' => "",
//The record offset from which to start.
'offset' => '0',
//Optional. A list of fields to include in the results.
'select_fields' => array(
'id',
'name',
'title',
),
/*
A list of link names and the fields to be returned for each link name.
Example: 'link_name_to_fields_array' => array(array('name' => 'email_addresses', 'value' => array('id', 'email_address', 'opt_out', 'primary_address')))
*/
'link_name_to_fields_array' => array(
),
//The maximum number of results to return.
'max_results' => '2',
//To exclude deleted records
'deleted' => '0',
//If only records marked as favorites should be returned.
'Favorites' => false,
);
$get_entry_list_result = call('get_entry_list', $get_entry_list_parameters, $url);
echo '<pre>';
print_r($get_entry_list_result);
echo '</pre>';
?>Result
stdClass Object
(
[result_count] => 2
[total_count] => 200
[next_offset] => 2
[entry_list] => Array
(
[0] => stdClass Object
(
[id] => 18124607-69d1-b158-47ff-4f7cb69344f7
[module_name] => Leads
[name_value_list] => stdClass Object
(
[id] => stdClass Object
(
[name] => id
[value] => 18124607-69d1-b158-47ff-4f7cb69344f7
)
[name] => stdClass Object
(
[name] => name
[value] => Bernie Worthey
)
[title] => stdClass Object
(
[name] => title
[value] => Senior Product Manager
)
)
)
[1] => stdClass Object
(
[id] => 1cdfddc1-2759-b007-8713-4f7cb64c2e9c
[module_name] => Leads
[name_value_list] => stdClass Object
(
[id] => stdClass Object
(
[name] => id
[value] => 1cdfddc1-2759-b007-8713-4f7cb64c2e9c
)
[name] => stdClass Object
(
[name] => name
[value] => Bobbie Kohlmeier
)
[title] => stdClass Object
(
[name] => title
[value] => Director Operations
)
)
)
)
[relationship_list] => Array
(
)
) 
Related
Topics
- Module Loader Restriction Workarounds (100%)
- Creating Documents - SOAP and PHP (100%)
- Introduction to the Manifest File
- Creating an Installable Package for a Logic Hook
- Sugar 6.7.x Supported Platforms
- Sugar 6.5.x Supported Platforms
- Sugar 6.6.x Supported Platforms
- What Version of the API Should I Be Using?
- Creating Documents - REST and PHP
- About PHP Notices, Warnings, and Errors
Tutorials
- Creating or Updating a Record - SOAP and C# (100%)
- Logging In - SOAP and C# (100%)
- Creating a Custom Quote Template (50%)
- Retrieving Multiple Records by ID From a Module - REST and PHP
- Retrieving Related Records - REST and PHP
- Logging In - SOAP and PHP
- Retrieving Related Records - SOAP and PHP
- Creating an Installable Package That Creates New Fields
- Retrieving a List of Records - SOAP and PHP
- Retrieving Multiple Records by ID - SOAP and PHP
Troubleshooting
- Common Performance Tweaks (100%)
- Troubleshooting a Blank Page When Running a Report
- Troubleshooting Being Unable to Log In to Sugar
- Troubleshooting License Validation
References
- Does Sugar Have an API? (100%)
