Where to call and how to call SugarCRM API for CRUD operation

Hello,

I am using sugar enterprise.

Where to call and how to call API in sugarcrm.I have both A and B instances, on Sugar Ent

I need to create a record in Instance A, account module same time that record should be create in Instance B, account module.

I created mytestcallA.php file where i put below code:

---------------------------------------------

$url = "">mytest.sugarondemand.com/.../rest.php";  //Instance B's URL
$username = "xyz.xyz";
$password = "pass@12";

//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;
$GLOBALS['log']->fatal("Session::".$session_id);

-----------------------------------------------------------------

Is above file code is correct for login?

Please help me.

Thank you

Parents
  • Hi Shivshankar,

    If you're using the latest APIs, you can use oauth2 tokens for your requests.

    To acquire a new token you could do something like this:

    curl -X POST -H Cache-Control:no-cache -H "Content-Type: application/json" -d '{ 
        "grant_type":"password", 
        "client_id":"sugar", 
        "client_secret":"", 
        "username":"<username>", 
        "password":"<password>", 
        "platform":"custom" 
    }' https:/<site_url>/rest/<version>/oauth2/token
    
    ==== RESPONSE
    {
        "access_token": "5ee48ec7-023e-ecff-5184-530bd0358868",
        "expires_in": 3600,
        "token_type": "bearer",
        "scope": null,
        "refresh_token": "5f197357-0167-f7a6-7912-530bd03275b6",
        "refresh_expires_in": 1209600,
        "download_token": "5f531625-e301-e3ea-1b11-530bd098be41"
    }

    With that token you can add as a header for your subsequent requests.

    axios.post('https://<site_url>/rest/<version>/Accounts',
        ACCOUNT_JSON,
      { 
        headers: {
          'content-type' : 'application/json', 
          'OAuth-Token' : '<token_from_oauth2>'
        }
      })

    Check out SugarCRM's integration guide and our CRUD docs too.

    Rafa

    SugarCRM | Principal Developer Advocate

  • Thank You for your valuable response Refael Fernandes !

    Can you please mention, where to call below code ,I mean path,

    Should i create a new file for that or call on any action of account like save?

    -----------------------------------------------------------------------------------------------------------------

    curl -X POST -H Cache-Control:no-cache -H "Content-Type: application/json" -d '{
    "grant_type":"password",
    "client_id":"sugar",
    "client_secret":"",
    "username":"<username>",
    "password":"<password>",
    "platform":"custom"
    }' https:/<site_url>/rest/<version>/oauth2/token

    -------------------------------------------------------------------------------------------------------------------------

    Thank You

  • Hi Shiv,

    This snippet is a CURL post directly to the URL, you could definitely take that and translate it to PHP/Node or whatever you're using.

    In PHP, would be something like this (it's a working code I did it here real quick):

    <?php
    
    // The data to send to the API
    $postData = array(
        'grant_type' => 'password',
        'client_id' => 'sugar',
        'client_secret' => '',
        'username' => 'YOUR_USER',
        'password' => 'YOUR_PASSWORD',
        'platform' => 'base'
    );
    
    // Create the context for the request
    $context = stream_context_create(array(
        'http' => array(
            // http://www.php.net/manual/en/context.http.php
            'method' => 'POST',
            'header' => "Content-Type: application/json\r\n",
            'content' => json_encode($postData)
        )
    ));
    
    // Send the request
    $response = file_get_contents('http://YOUR_URL/sugar/rest/v11_13/oauth2/token', FALSE, $context);
    
    // Check for errors
    if($response === FALSE){
        die('Error');
    }
    
    // Decode the response
    $responseData = json_decode($response, TRUE);
    
    // Print the access token from the response
    echo $responseData['access_token'];

    SugarCRM | Principal Developer Advocate

Reply
  • Hi Shiv,

    This snippet is a CURL post directly to the URL, you could definitely take that and translate it to PHP/Node or whatever you're using.

    In PHP, would be something like this (it's a working code I did it here real quick):

    <?php
    
    // The data to send to the API
    $postData = array(
        'grant_type' => 'password',
        'client_id' => 'sugar',
        'client_secret' => '',
        'username' => 'YOUR_USER',
        'password' => 'YOUR_PASSWORD',
        'platform' => 'base'
    );
    
    // Create the context for the request
    $context = stream_context_create(array(
        'http' => array(
            // http://www.php.net/manual/en/context.http.php
            'method' => 'POST',
            'header' => "Content-Type: application/json\r\n",
            'content' => json_encode($postData)
        )
    ));
    
    // Send the request
    $response = file_get_contents('http://YOUR_URL/sugar/rest/v11_13/oauth2/token', FALSE, $context);
    
    // Check for errors
    if($response === FALSE){
        die('Error');
    }
    
    // Decode the response
    $responseData = json_decode($response, TRUE);
    
    // Print the access token from the response
    echo $responseData['access_token'];

    SugarCRM | Principal Developer Advocate

Children