一个PHP写的简单webservice服务端+客户端

首先是服务端,服务端有一个主要的class组成:apiServer.php

<?php /** * apiServer.php * * webservice主类 * * @filename apiServer.php * @version v1.0 * @update 2011-12-22 * @author homingway * @contact homingway@gmail.com * @package webservice */ define(\'API_AUTH_KEY\', \'i8XsJb$fJ!87FblnW\'); class apiServer{ //请求参数 public $request = array(); //是否ip限制 public $ip_limit = true; //允许访问的IP列表 public $ip_allow = array(\'127.0.0.1\',\'192.168.0.99\'); public $default_method = \'welcome.index\'; public $service_method = array(); //私有静态单例变量 private static $_instance = null; /** * 构造方法,处理请求参数 */ private function __construct(){ $this->dealRequest(); } /** * 单例运行 */ public static function getInstance(){ if(self::$_instance === null){ self::$_instance = new self(); } return self::$_instance; } /** * 运行 */ public function run(){ //授权 if(!$this->checkAuth()){ exit(\'3|Access Denied\'); } $this->getApiMethod(); include_once(API_SERVICE_PATH.\'/\'.$this->service_method[\'service\'].\'.php\'); $serviceObject = new $this->service_method[\'service\']; if($this->request[\'param\']){ $result = call_user_func_array(array($serviceObject,$this->service_method[\'method\']),$this->request[\'param\']); } else { $result = call_user_func(array($serviceObject,$this->service_method[\'method\'])); } if(is_array($result)){ $result = json_encode($result); } $result = gzencode($result); exit($result); } /** * 检查授权 */ public function checkAuth(){ //检查参数是否为空 if(!$this->request[\'time\'] || !$this->request[\'method\'] || !$this->request[\'auth\']){ return false; } //检查auth是否正确 $server_auth = md5(md5($this->request[\'time\'].\'|\'.$this->request[\'method\'].\'|\'.API_AUTH_KEY)); if($server_auth != $this->request[\'auth\']){ return false; } //ip限制 if($this->ip_limit){ $remote_ip = $this->getIP(); $intersect = array_intersect($remote_ip,$this->ip_allow); if(empty($intersect)){ return false; } } return true; } /** * 获取服务名和方法名 */ public function getApiMethod(){ if(strpos($this->request[\'method\'], \'.\') === false){ $method = $this->default_method; } else { $method = $this->request[\'method\']; } $tmp = explode(\'.\', $method); $this->service_method = array(\'service\'=>$tmp[0],\'method\'=>$tmp[1]); return $this->service_method; } /** * 获取和处理请求参数 */ public function dealRequest(){ $this->request[\'time\'] = $this->_request(\'time\'); $this->request[\'method\'] = $this->_request(\'method\'); $this->request[\'param\'] = $this->_request(\'param\'); $this->request[\'auth\'] = $this->_request(\'auth\'); if($this->request[\'param\']){ $this->request[\'param\'] = json_decode(urldecode($this->request[\'param\']),true); } } /** * 获取request变量 * @param string $item */ private function _request($item){ return isset($_REQUEST[$item]) ? trim($_REQUEST[$item]) : \'\'; } /** * 设置IP限制 * @param bool $limit */ public function setIPLimit($limit=true){ $this->ip_limit = $limit; } /** * 获取客户端ip地址 */ public function getIP(){ $ip = array(); if(isset($_SERVER[\'REMOTE_ADDR\'])){ $ip[] = $_SERVER[\'REMOTE_ADDR\']; } if(isset($_SERVER[\'HTTP_VIA\'])){ $tmp = explode(\', \',$_SERVER[\'HTTP_X_FORWARDED_FOR\']); $ip = array_merge($ip,$tmp); } $ip = array_unique($ip); return $ip; } }
?>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzjxwf.html