5 min read

Get list of records from SugarCRM module using REST API

Get list of records from SugarCRM module using REST API

Last modified

Its very common problem for many people to fetch data from SugarCRM application and feed it to some other application. Using REST API we can easily fetch list of records from any SugarCRM module. This tutorial show you how can you create your own API endpoint. In the example below, you’ll need to replace the URL, Username, Password, then locate ‘{Module}’ and replace it with the corresponding module name. And if you want a list of records from Projects, you would replace {Module} with Projects.


<?php

$base_url = "http://{URL}/rest/v10";
$username = "{Username}";
$password = "{Password}";

/**
 * Generic function to make cURL request.
 * @param $url - The URL route to use.
 * @param string $oauthtoken - The oauth token.
 * @param string $type - GET, POST, PUT, DELETE. Defaults to GET.
 * @param array $arguments - Endpoint arguments.
 * @param array $encodeData - Whether or not to JSON encode the data.
 * @param array $returnHeaders - Whether or not to return the headers.
 * @return mixed
 */
function call(
    $url,
    $oauthtoken='',
    $type='GET',
    $arguments=array(),
    $encodeData=true,
    $returnHeaders=false
)
{
    $type = strtoupper($type);

    if ($type == 'GET')
    {
        $url .= "?" . http_build_query($arguments);
    }

    $curl_request = curl_init($url);

    if ($type == 'POST')
    {
        curl_setopt($curl_request, CURLOPT_POST, 1);
    }
    elseif ($type == 'PUT')
    {
        curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "PUT");
    }
    elseif ($type == 'DELETE')
    {
        curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "DELETE");
    }

    curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($curl_request, CURLOPT_HEADER, $returnHeaders);
    curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);

    if (!empty($oauthtoken))
    {
        $token = array("oauth-token: {$oauthtoken}");
        curl_setopt($curl_request, CURLOPT_HTTPHEADER, $token);
    }

    if (!empty($arguments) && $type !== 'GET')
    {
        if ($encodeData)
        {
            //encode the arguments as JSON
            $arguments = json_encode($arguments);
        }
        curl_setopt($curl_request, CURLOPT_POSTFIELDS, $arguments);
    }

    $result = curl_exec($curl_request);

    if ($returnHeaders)
    {
        //set headers from response
        list($headers, $content) = explode("\r\n\r\n", $result ,2);
        foreach (explode("\r\n",$headers) as $header)
        {
            header($header);
        }

        //return the nonheader data
        return trim($content);
    }

    curl_close($curl_request);

    //decode the response from JSON
    $response = json_decode($result);

    return $response;
}

//Login - POST /oauth2/token

$url = $base_url . "/oauth2/token";

$oauth2_token_arguments = array(
    "grant_type" => "password",
    //client id/secret you created in Admin > OAuth Keys
    "client_id" => "<CustomID>",
    "client_secret" => "<CustomSecret>",
    "username" => $username,
    "password" => $password,
    "platform" => "base"
);

$oauth2_token_response = call($url, '', 'POST', $oauth2_token_arguments);

//Return records - /<module> GET
$url = $base_url . "/{Modlule}";

$filter_response = call($url, $oauth2_token_response->access_token, 'GET');

echo "<pre>";
print_r($filter_response);
echo "</pre>";

?>

You’ll need to copy this code into a ‘.php’ file. Once you’ve updated all of the ‘{ }’ items, you can move the file to your web server’s root directory. Then, using a web browser, you can navigate to the file to run it.

The above script will GET a list of records from the specified module. For more information about the REST v10 API, you can also navigate to the following location: {Your SugarCRM URL}/rest/v10/help

If you’re connecting to an HTTPS link, the http:// needed to be updated to https:// Also, because the API script provided above uses OAuth, you need to specify the Consumer Key (client_id) and Consumer Secret Key (client_secret) values that are available if you go to Admin > OAuth Keys.

The Sugar Support site provides a number of REST v10 Examples written in PHP that you can use: http://support.sugarcrm.com/02_Documentation/04_Sugar_Developer/Sugar_Developer_Guide_7.2/70_API/Web…