{"id":9,"date":"2016-05-05T11:50:48","date_gmt":"2016-05-05T11:50:48","guid":{"rendered":"http:\/\/symbioticindia.in\/docu\/?p=9"},"modified":"2016-05-06T08:46:52","modified_gmt":"2016-05-06T08:46:52","slug":"get-list-of-records-from-sugarcrm-module-using-rest-api","status":"publish","type":"post","link":"http:\/\/symbioticindia.in\/docu\/2016\/05\/05\/get-list-of-records-from-sugarcrm-module-using-rest-api\/","title":{"rendered":"Get list of records from SugarCRM module using REST API"},"content":{"rendered":"<p>Its very common problem for many people to fetch data from <a href=\"http:\/\/scriptbaker.com\/get-list-of-records-from-sugarcrm-module-using-api\/www.sugarcrm.com\">SugarCRM<\/a>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\u2019ll need to replace the URL, Username, Password, then locate \u2018{Module}\u2019 and replace it with the corresponding module name. And if you want a list of records from Projects, you would replace {Module} with Projects.<br \/>\n<span id=\"more-798\"><\/span><\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">&lt;?php\r\n\r\n$base_url = \"http:\/\/{URL}\/rest\/v10\";\r\n$username = \"{Username}\";\r\n$password = \"{Password}\";\r\n\r\n\/**\r\n * Generic function to make cURL request.\r\n * @param $url - The URL route to use.\r\n * @param string $oauthtoken - The oauth token.\r\n * @param string $type - GET, POST, PUT, DELETE. Defaults to GET.\r\n * @param array $arguments - Endpoint arguments.\r\n * @param array $encodeData - Whether or not to JSON encode the data.\r\n * @param array $returnHeaders - Whether or not to return the headers.\r\n * @return mixed\r\n *\/\r\nfunction call(\r\n    $url,\r\n    $oauthtoken='',\r\n    $type='GET',\r\n    $arguments=array(),\r\n    $encodeData=true,\r\n    $returnHeaders=false\r\n)\r\n{\r\n    $type = strtoupper($type);\r\n\r\n    if ($type == 'GET')\r\n    {\r\n        $url .= \"?\" . http_build_query($arguments);\r\n    }\r\n\r\n    $curl_request = curl_init($url);\r\n\r\n    if ($type == 'POST')\r\n    {\r\n        curl_setopt($curl_request, CURLOPT_POST, 1);\r\n    }\r\n    elseif ($type == 'PUT')\r\n    {\r\n        curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, \"PUT\");\r\n    }\r\n    elseif ($type == 'DELETE')\r\n    {\r\n        curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, \"DELETE\");\r\n    }\r\n\r\n    curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);\r\n    curl_setopt($curl_request, CURLOPT_HEADER, $returnHeaders);\r\n    curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);\r\n    curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);\r\n    curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);\r\n\r\n    if (!empty($oauthtoken))\r\n    {\r\n        $token = array(\"oauth-token: {$oauthtoken}\");\r\n        curl_setopt($curl_request, CURLOPT_HTTPHEADER, $token);\r\n    }\r\n\r\n    if (!empty($arguments) &amp;&amp; $type !== 'GET')\r\n    {\r\n        if ($encodeData)\r\n        {\r\n            \/\/encode the arguments as JSON\r\n            $arguments = json_encode($arguments);\r\n        }\r\n        curl_setopt($curl_request, CURLOPT_POSTFIELDS, $arguments);\r\n    }\r\n\r\n    $result = curl_exec($curl_request);\r\n\r\n    if ($returnHeaders)\r\n    {\r\n        \/\/set headers from response\r\n        list($headers, $content) = explode(\"\\r\\n\\r\\n\", $result ,2);\r\n        foreach (explode(\"\\r\\n\",$headers) as $header)\r\n        {\r\n            header($header);\r\n        }\r\n\r\n        \/\/return the nonheader data\r\n        return trim($content);\r\n    }\r\n\r\n    curl_close($curl_request);\r\n\r\n    \/\/decode the response from JSON\r\n    $response = json_decode($result);\r\n\r\n    return $response;\r\n}\r\n\r\n\/\/Login - POST \/oauth2\/token\r\n\r\n$url = $base_url . \"\/oauth2\/token\";\r\n\r\n$oauth2_token_arguments = array(\r\n    \"grant_type\" =&gt; \"password\",\r\n    \/\/client id\/secret you created in Admin &gt; OAuth Keys\r\n    \"client_id\" =&gt; \"&lt;CustomID&gt;\",\r\n    \"client_secret\" =&gt; \"&lt;CustomSecret&gt;\",\r\n    \"username\" =&gt; $username,\r\n    \"password\" =&gt; $password,\r\n    \"platform\" =&gt; \"base\"\r\n);\r\n\r\n$oauth2_token_response = call($url, '', 'POST', $oauth2_token_arguments);\r\n\r\n\/\/Return records - \/&lt;module&gt; GET\r\n$url = $base_url . \"\/{Modlule}\";\r\n\r\n$filter_response = call($url, $oauth2_token_response-&gt;access_token, 'GET');\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($filter_response);\r\necho \"&lt;\/pre&gt;\";\r\n\r\n?&gt;\r\n<\/pre>\n<p>You\u2019ll need to copy this code into a \u2018.php\u2019 file. Once you\u2019ve updated all of the \u2018{ }\u2019 items, you can move the file to your web server\u2019s root directory. Then, using a web browser, you can navigate to the file to run it.<\/p>\n<p>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<\/p>\n<p>If you\u2019re 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 &gt; OAuth Keys.<\/p>\n<p>The Sugar Support site provides a number of REST v10 Examples written in PHP that you can use:<a title=\"Sugar API V10\" href=\"http:\/\/support.sugarcrm.com\/02_Documentation\/04_Sugar_Developer\/Sugar_Developer_Guide_7.2\/70_API\/Web_Services\/20_Examples\/v10\/\" target=\"_blank\">http:\/\/support.sugarcrm.com\/02_Documentation\/04_Sugar_Developer\/Sugar_Developer_Guide_7.2\/70_API\/Web\u2026<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Its very common problem for many people to fetch data from SugarCRMapplication and feed it to some other application. Using REST API we can easily fetch list of\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2],"tags":[],"class_list":["post-9","post","type-post","status-publish","format-standard","hentry","category-asterisk","category-sugarcrm"],"_links":{"self":[{"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/posts\/9","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/comments?post=9"}],"version-history":[{"count":1,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/posts\/9\/revisions"}],"predecessor-version":[{"id":10,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/posts\/9\/revisions\/10"}],"wp:attachment":[{"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/media?parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/categories?post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/symbioticindia.in\/docu\/wp-json\/wp\/v2\/tags?post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}