Skip to content

Instantly share code, notes, and snippets.

@elchele
Created March 26, 2015 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elchele/694d50c518d83c2934a7 to your computer and use it in GitHub Desktop.
Save elchele/694d50c518d83c2934a7 to your computer and use it in GitHub Desktop.
Custom endpoint for Sugar REST API, uses CURL to interact with external web service.
<?php
/* Author: Angel Magaña -- cheleguanaco@cheleguanaco.com
* File: ./custom/modules/Accounts/clients/base/api/StockApi.php
*
* Custom Sugar REST API endpoint demonstrating use of CURL to
* interact with an external web service.
*
* Useful for circumventing XSS issues one may experience when calling
* a web service via JavaScript (e.g. Sidecar controller)
*/
//Class name should match filename
class StockApi extends SugarApi {
//This method must be defined in all custom endpoints
public function registerApiRest() {
return array(
'getStockData' => array(
'reqType' => 'GET', //The type of web requests this code responds to
'path' => array('ticker','?'),
'pathVars' => array('','symbol'),
'method' => 'getStockData', //The PHP method that will execute for the response
'shortHelp' => 'This method retrieves stock data',
'longHelp' => '',
)
);
}
//This is the method that executes when the endpoint is invoked
public function getStockData($api, $args)
{
$symbol = $args['symbol'];
$url = 'http://dev.markitondemand.com/Api/v2/Quote/json?symbol=' . $symbol;
//cURL Request
$cu = curl_init();
curl_setopt($cu, CURLOPT_URL, $url);
curl_setopt($cu, CURLOPT_HEADER, 0);
curl_setopt($cu, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($cu);
curl_close($cu);
return $result;
}
}
?>
@elchele
Copy link
Author

elchele commented Mar 26, 2015

You must execute a "Quick Repair and Rebuild" within your instance in order for the above code to apply.

The resulting endpoint would be:

GET /rest/v10/ticker/:symbol

Where :symbol represents the ticker symbol to query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment