Converting Custom Entry Points into Rest API

By
123 views

Using Rest API calls as an alternative to Entry points is an intelligent practice which increases overall efficiency of Web service. To Customize Rest API, see the example given below: (The example has been taken from official SugarCRM’s Developer Guide for Ping functionality. You can modify it as per your functionality requirements.)

Example

<?php

$base_url = “//{site_url}/rest/v10”;

$username = “admin”;

$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(“rnrn”, $result ,2);

foreach (explode(“rn”,$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);

//Ping – GET /ping

$url = $base_url . “/ping”;

$ping_response = call($url, $oauth2_token_response->access_token, ‘GET’);

echo “<pre>”;

print_r($ping_response);

echo “</pre>”;

?>

Results

Request

This endpoint does not accept any request arguments.

Response

“pong”

Need Help? Get Free Consultation


Please leave this field empty.

Get in touch



Please leave this field empty.

CONTACT INFORMATION