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
- /**
- * Mini REST call
- *
- * @ Param mixed $ url REST server url
- * @ Param mixed $ method
- * @ Param array $ params parameter
- * @ Param mixed $ 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 );
- }
- }
- }
- 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'';
- }
|