Mini REST client and server

Source: Internet
Author: User
Mini REST client and server
REST is used in the ongoing project, and the REST and CURL libraries are used in the project. this is my simplified client and server. please correct me.

Mini_rest_call.php client
Rest_server.php server

  1. /**
  2. * Mini REST call
  3. *
  4. * @ Param mixed $ url REST server url
  5. * @ Param mixed $ method
  6. * @ Param array $ params parameter
  7. * @ Param mixed $ request method (get, post, put, delete)
  8. * @ Param mixed $ request return format (json, xml)
  9. *
  10. * @ Author PiscDong (http://www.piscdong.com /)
  11. */
  12. Function mini_rest_call ($ url, $ method = '', $ params = array (), $ request = 'get', $ format = 'json '){
  13. If (substr ($ url,-1 )! = '/' & Substr ($ method, 0, 1 )! = '/') $ Url. = '/';
  14. $ Url. = $ method;
  15. $ Postfields = http_build_query ($ params );
  16. If ($ format! = 'Xml') $ format = 'json ';
  17. If ($ request! = 'Post' & $ request! = 'Put' & $ request! = 'Delete') $ request = 'get ';
  18. $ Ci = curl_init ();
  19. Curl_setopt ($ ci, CURLOPT_SSL_VERIFYPEER, FALSE );
  20. Curl_setopt ($ ci, CURLOPT_RETURNTRANSFER, 1 );
  21. Curl_setopt ($ ci, CURLOPT_CONNECTTIMEOUT, 30 );
  22. Curl_setopt ($ ci, CURLOPT_TIMEOUT, 30 );
  23. Switch ($ request ){
  24. Case 'get ':
  25. $ Url. = '? '. $ Postfields;
  26. Break;
  27. Case 'Post ':
  28. Curl_setopt ($ ci, CURLOPT_POST, TRUE );
  29. Curl_setopt ($ ci, CURLOPT_POSTFIELDS, $ postfields );
  30. Break;
  31. Case 'put ':
  32. Curl_setopt ($ ci, CURLOPT_CUSTOMREQUEST, 'put ');
  33. Curl_setopt ($ ci, CURLOPT_POSTFIELDS, $ postfields );
  34. $ Headers [] = 'x-HTTP-Method-Override: put ';
  35. Break;
  36. Case 'delete': // Not Tested
  37. Curl_setopt ($ ci, CURLOPT_CUSTOMREQUEST, 'delete ');
  38. Curl_setopt ($ ci, CURLOPT_POSTFIELDS, $ postfields );
  39. Break;
  40. }
  41. $ Headers [] = 'User-Agent: mini_rest_client (piscdong.com )';
  42. $ Headers [] = 'accept: application/'. $ format;
  43. Curl_setopt ($ ci, CURLOPT_HTTPHEADER, $ headers );
  44. Curl_setopt ($ ci, CURLOPT_URL, $ url );
  45. $ Response = curl_exec ($ ci );
  46. Curl_close ($ ci );
  47. If ($ response! = ''){
  48. If ($ format = 'json '){
  49. Return json_decode ($ response, true );
  50. } Else {
  51. Return simplexml_load_string ($ response );
  52. }
  53. }
  54. }

  1. Function array2xml ($ array ){
  2. $ Xml = '';
  3. Foreach ($ array as $ k => $ v ){
  4. $ Xml. = '<'. $ k. '> ';
  5. If (is_array ($ v )){
  6. $ Xml. = array2xml ($ v );
  7. } Else {
  8. $ Xml. = $ v;
  9. }
  10. $ Xml. =' ';
  11. }
  12. Return $ xml;
  13. }
  14. $ Format = 'json ';
  15. If (isset ($ _ SERVER ['http _ ACCEPT ']) & $ _ SERVER ['http _ ACCEPT'] = 'application/XML ') $ format = 'xml ';
  16. $ Return ['format'] = $ format;
  17. $ Method = '';
  18. If (isset ($ _ SERVER ['path _ info']) & $ _ SERVER ['path _ info']! = ''){
  19. If (substr ($ _ SERVER ['path _ info'], 0, 1) = '/') $ method = substr ($ _ SERVER ['path _ info'], 1 );
  20. }
  21. If ($ method! = ''){
  22. $ Return ['method'] = $ method;
  23. } Else {
  24. $ Return ['method _ err'] = 'no method ';
  25. }
  26. $ Request = '';
  27. If (isset ($ _ SERVER ['request _ method']) & $ _ SERVER ['request _ method']! = ''){
  28. Switch ($ _ SERVER ['request _ method']) {
  29. Case 'get ':
  30. $ Request = 'get ';
  31. Foreach ($ _ GET as $ k => $ v) $ return ['param _ '. $ k] = $ v;
  32. Break;
  33. Case 'Post ':
  34. $ Request = 'post ';
  35. Foreach ($ _ POST as $ k =>$ v) $ return ['param _ '. $ k] = $ v;
  36. Break;
  37. Case 'put ':
  38. $ Request = 'put ';
  39. $ Param = file_get_contents ("php: // input ");
  40. If ($ param! = ''){
  41. Parse_str ($ param, $ param_r );
  42. Foreach ($ param_r as $ k => $ v) $ return ['param _ '. $ k] = $ v;
  43. }
  44. Break;
  45. Case 'delete ':
  46. $ Request = 'delete ';
  47. $ Param = file_get_contents ("php: // input ");
  48. If ($ param! = ''){
  49. Parse_str ($ param, $ param_r );
  50. Foreach ($ param_r as $ k => $ v) $ return ['param _ '. $ k] = $ v;
  51. }
  52. Break;
  53. }
  54. }
  55. If ($ request! = ''){
  56. $ Return ['request'] = $ request;
  57. } Else {
  58. $ Return ['request _ err'] = 'No request ';
  59. }
  60. If ($ format = 'json '){
  61. Echo json_encode ($ return );
  62. } Else {
  63. Echo' ';
  64. Echo array2xml ($ return );
  65. Echo'';
  66. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.