Mini REST client and server

WBOY
Release: 2016-07-25 08:50:58
Original
847 people have browsed it
REST is used in the project I am working on. The two libraries REST and CURL are used in the project. This is the client and server that I simplified after research. Please correct me

mini_rest_call.php client
rest_server.php server end
  1. /**
  2. * mini REST call
  3. *
  4. * @param mixed $url REST server-side URL
  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


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!