Get list of records from SugarCRM module using REST API

Get list of records from Sugar CRM module using API

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…

Author: Tahir Yasin

Tahir is a Passionate Web Developer from Lahore, Pakistan and a guy with NEVER-STOP-LEARNING attitude who keeps an eye on emerging technologies and customer trends.

3 thoughts on “Get list of records from SugarCRM module using REST API”

  1. how get all fields to custom module and display to user and select the fields that require but apply a change.

    example the user select 3 field in the page custom, after execute function clear special character only field selected for user.

    It is posible?

Leave a Reply

Your email address will not be published. Required fields are marked *