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
- /**
- * mini REST call
- *
- * @param mixed $url REST server-side URL
- * @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.=' '.$k.'>';
- }
- 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
|