Using the SugarCRM REST API

35,088 views

Published on

Published in: Technology
4 Comments
11 Likes
Statistics
Notes
No Downloads
Views
Total views
35,088
On SlideShare
0
From Embeds
0
Number of Embeds
1,930
Actions
Shares
0
Downloads
335
Comments
4
Likes
11
Embeds 0
No embeds

No notes for slide
  • \n
  • milsoft\ncomputer science major\nopen source web development and clean web design\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Using the SugarCRM REST API

    1. 1. Using the SugarCRM REST API
    2. 2. About the Speaker Asa Kusuma• Intern at Milsoft Utility Solutions• Junior at Abilene Christian University• Twitter: asakusuma• asakusuma.com
    3. 3. What is a REST API?• Web service • Allow external access to data • Some protocol for transferring data• Representational State Transfer• Transfer an object’s “state” across apps• Encode with JSON, send using HTTPS
    4. 4. REST vs. SOAP• REST photo by superbomba • JSON • Less overhead• SOAP • XML • More overhead
    5. 5. Web Services:What’s the Big Deal?
    6. 6. photo by gerlos www.photographyicon.comBridge apps with data
    7. 7. CRM needs external dataExternal apps need CRM data
    8. 8. Examples• Customers update their own data photo by eBeam• Product support • Customers access cases • Add notes with attachments• Custom modules = endless possibility photo by Sprengben
    9. 9. End Goal:Build a web app thattalks with SugarCRM
    10. 10. The End Goal Expanded• CRUD capability photo by eBeam• PHP web app • cURL Library • JSON • Wrapper class• Security photo by Sprengben
    11. 11. The Big Picture PHP App SugarCRM Model DataREST Wrapper REST API Class
    12. 12. Get the codeasakusuma.com/sugarcon
    13. 13. Ideal Code Example photo by eBeam$sugar = new SugarREST();$module = “cases”;$fields = array(‘id’,‘name’,‘status’);$records = $sugar->get($module,$fields);foreach($records as $record) { echo $record[‘name’];}
    14. 14. But first, some REST API basics
    15. 15. JSON Primerarray( photo by eBeam ‘name’ => ‘Robert’, ‘children’ => array( ‘Chris’, ‘Anne’, ‘Bob’ ))
    16. 16. JSON Primer (cont.){ photo by eBeam “name”:“Robert”, “children” : { 0:“Chris”, 1:“Anne”, 2:“Bob” }}
    17. 17. JSON Primer (cont.) photo by eBeam$my_array = array(1,2,3);$my_json_str = json_encode($my_array);$my_array = json_decode($my_json_string);//$my_array remains unchanged
    18. 18. Making REST API Calls• https://mysite.com/sugarcrm/service/v2/rest.php• Method name• Input type• Response type• Arguments (JSON)
    19. 19. Example POST Datamethod=login&input_type=json&response_type=json&rest_data={"name": “value”}
    20. 20. Implemented in PHP$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $rest_api_url);//Don’t require SSL certificatecurl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//Return the result datacurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);$output = json_decode(curl_exec($ch));
    21. 21. Authentication1. Make “Login” REST call photo by eBeam2. Receive session ID3. Use session ID to authenticate other calls4. Make “Logout” REST call with session ID5. Use HTTPS
    22. 22. Example: Modify Contact
    23. 23. Modify Contact (cont.)$args = array( session => $session_id, module_name => Contacts, name_value_list => array( id=>9hg5a93a-2dfdf-d6pe-1oq4-cjqic4mxok, title=>REST API Guy ));
    24. 24. Modify Contact (cont.)$post_data = "method=set_entry&";$post_data .= "input_type=json&";$post_data .= "response_type=json&";$post_data .= "rest_data=".json_encode($args); $ch = curl_init();curl_setopt($ch, CURLOPT_URL, $rest_api_url);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);$output = json_decode(curl_exec($ch));
    25. 25. REST Wrapper Class1. Download from asakusuma.com/sugarcon photo by eBeam • example.php • sugar_rest.php • README2. Read the README (it’s there for a reason)3. Configure sugar_rest.php4. Run the example
    26. 26. Important Functions//set() is both create and updateset($module, $fields, $options);get($module, $fields, $options);get_with_related($module, $fields,$options);
    27. 27. Note Functions• set_note_attachment()• get_note_attachment()• Use binary data• Arguments are a little complicated• Comment documentation in sugar_rest.php
    28. 28. Security photo by maistora• Protect your data• Weakest link• REST API = liability• Use caution when mapping URL to data
    29. 29. photo by gerlosLeverage your data. www.photographyicon.comConnect your apps.
    30. 30. Presentation + Code at asakusuma.com Official Docs atdevelopers.sugarcrm.com

    ×