欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

迷你REST客户端及服务器端

程序员文章站 2022-05-17 11:55:28
...
正在做的项目中要用到REST,项目中用到了REST和CURL两个库,这是我研究后自己简化的客户端及服务器端,请大家指正

mini_rest_call.php 客户端
rest_server.php 服务器端
  1. /**
  2. * mini REST call
  3. *
  4. * @param mixed $url REST服务器端网址
  5. * @param mixed $method 方法
  6. * @param array $params 参数
  7. * @param mixed $request 请求方式(get, post, put, delete)
  8. * @param mixed $request 返回格式(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': //未测试
  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.='';
  5. if(is_array($v)){
  6. $xml.=array2xml($v);
  7. }else{
  8. $xml.=$v;
  9. }
  10. $xml.=''.$k.'>';
  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. ';
  67. }
复制代码