Created
March 26, 2015 21:01
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Author: Angel Magaña -- [email protected] | |
* 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; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.