正在做的项目中要用到REST,项目中用到了REST和CURL两个库,这是我研究后自己简化的客户端及服务器端,请大家指正
mini_rest_call.php 客户端 rest_server.php 服务器端
-
/**
- * mini REST call
- *
- * @param mixed $url REST服务器端网址
- * @param mixed $method 方法
- * @param array $params 参数
- * @param mixed $request 请求方式(get, post, put, delete)
- * @param mixed $request 返回格式(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': //未测试
- 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.='';
- 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 '
';
- }
-
复制代码
|