Mini rest client and server side

Source: Internet
Author: User
Rest is used in the project, and the rest and curl two libraries are used in the project, which is my own simplified client and server side after I researched it, please correct me.

mini_rest_call.php Client
rest_server.php Server-side
  1. /**
  2. * Mini REST Call
  3. *
  4. * @param mixed $url rest server-side URLs
  5. * @param mixed $method method
  6. * @param array $params parameters
  7. * @param mixed $request 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. }
Copy Code
  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. }
Copy Code
  • 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.