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
- /**
- * Mini REST Call
- *
- * @param mixed $url rest server-side URLs
- * @param mixed $method method
- * @param array $params parameters
- * @param mixed $request request method (GET, post, put, delete)
- * @param mixed $request return format (JSON, XML)
- *
- * @author Piscdong (http://www.piscdong.com/)
- */
- function Mini_rest_call ($url, $method = ", $params =array (), $request = ' get ', $format = ' json ') {
- if (substr ($url,-1)! = '/' && substr ($method, 0, 1) = = '/') $url. = '/';
- $url. = $method;
- $postfields =http_build_query ($params);
- if ($format! = ' xml ') $format = ' json ';
- if ($request! = ' Post ' && $request! = ' put ' && $request! = ' delete ') $request = ' get ';
- $ci =curl_init ();
- curl_setopt ($ci, Curlopt_ssl_verifypeer, FALSE);
- curl_setopt ($ci, Curlopt_returntransfer, 1);
- curl_setopt ($ci, Curlopt_connecttimeout, 30);
- curl_setopt ($ci, Curlopt_timeout, 30);
- Switch ($request) {
- Case ' get ':
- $url. = '? '. $postfields;
- Break
- Case ' post ':
- curl_setopt ($ci, Curlopt_post, TRUE);
- curl_setopt ($ci, Curlopt_postfields, $postfields);
- Break
- Case ' put ':
- curl_setopt ($ci, Curlopt_customrequest, ' PUT ');
- curl_setopt ($ci, Curlopt_postfields, $postfields);
- $headers []= ' x-http-method-override:put ';
- Break
- Case ' Delete '://Not tested
- curl_setopt ($ci, Curlopt_customrequest, ' DELETE ');
- curl_setopt ($ci, Curlopt_postfields, $postfields);
- Break
- }
- $headers []= ' User-agent:mini_rest_client (piscdong.com) ';
- $headers []= ' accept:application/'. $format;
- curl_setopt ($ci, Curlopt_httpheader, $headers);
- curl_setopt ($ci, Curlopt_url, $url);
- $response =curl_exec ($CI);
- Curl_close ($CI);
- if ($response! = ") {
- if ($format = = ' json ') {
- Return Json_decode ($response, true);
- }else{
- Return simplexml_load_string ($response);
- }
- }
- }
Copy Code
- function Array2xml ($array) {
- $xml = ";
- foreach ($array as $k = = $v) {
- $xml. = ' < ' $k. ' > ';
- if (Is_array ($v)) {
- $xml. =array2xml ($v);
- }else{
- $xml. = $v;
- }
- $xml. = ' ;
- }
- return $xml;
- }
- $format = ' json ';
- if (Isset ($_server[' http_accept ')) && $_server[' http_accept ']== ' application/xml ') $format = ' xml ';
- $return [' Format ']= $format;
- $method = ";
- if (Isset ($_server[' path_info ')) && $_server[' path_info ']!= ') {
- if (substr ($_server[' path_info '), 0, 1) = = '/') $method =substr ($_server[' Path_info '], 1);
- }
- if ($method! = ") {
- $return [' method ']= $method;
- }else{
- $return [' Method_err ']= ' no method ';
- }
- $request = ";
- if (Isset ($_server[' Request_method ')) && $_server[' request_method ']!= ') {
- Switch ($_server[' Request_method ')} {
- Case ' GET ':
- $request = ' get ';
- foreach ($_get as $k = $v) $return [' Param_ '. $k]= $v;
- Break
- Case ' POST ':
- $request = ' post ';
- foreach ($_post as $k = $v) $return [' Param_ '. $k]= $v;
- Break
- Case ' PUT ':
- $request = ' Put ';
- $param =file_get_contents ("Php://input");
- if ($param! = ") {
- Parse_str ($param, $param _r);
- foreach ($param _r as $k = + $v) $return [' Param_ '. $k]= $v;
- }
- Break
- Case ' DELETE ':
- $request = ' delete ';
- $param =file_get_contents ("Php://input");
- if ($param! = ") {
- Parse_str ($param, $param _r);
- foreach ($param _r as $k = + $v) $return [' Param_ '. $k]= $v;
- }
- Break
- }
- }
- if ($request! = ") {
- $return [' request ']= $request;
- }else{
- $return [' Request_err ']= ' no request ';
- }
- if ($format = = ' json ') {
- echo Json_encode ($return);
- }else{
- Echo ' ;
- echo Array2xml ($return);
- Echo ';
- }
Copy Code |