一个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;
} }
?>
然后在服务端的入口文件中调用该class,并启动服务即可,如:
<?php
/**
* server.php
*
* 自定义数据接口的入口
*
* @filename server.php
* @version v1.0
* @update 2011-12-22
* @author homingway
* @contact homingway@gmail.com
* @package webservice
*/ //API的根目录
define('API_PATH',dirname(__FILE__)); //服务目录
define('API_SERVICE_PATH',API_PATH.'/service');
define('API_LIB_PATH', API_PATH.'/lib'); //服务核心class
include_once(API_LIB_PATH.'/apiServer.php'); //运行
apiServer::getInstance()->run();
?>
然后创建一个service的目录,里面就是自己的接口class,如welcome.php:
<?php
/**
* welcome.php
*
* 功能代码
*
* @filename welcome.php
* @version v1.0
* @update 2011-12-22
* @author homingway
* @contact homingway@gmail.com
* @package webservice
*/ class welcome{ public function index(){
return 'hello service';
} }
?>
下面是客户端的主程序:apiClient.php
<?php
/**
* apiClient.php
*
* webservice客户端程序
*
* @filename apiClient.php
* @version v1.0
* @update 2011-12-22
* @author homingway
* @contact homingway@gmail.com
* @package webservice
*/ define('API_AUTH_KEY', 'i8XsJb$fJ!87FblnW'); class apiClient{ public static function send($url,$method,$param=array()){
$time = time();
$auth = md5(md5($time.'|'.$method.'|'.API_AUTH_KEY));
if(!is_array($param) || empty($param)){
$json_param = '';
} else {
$json_param = urlencode(json_encode($param));
}
$api_url = $url.'?method='.$method.'&time='.$time.'&auth='.$auth.'¶m='.$json_param;
$content = file_get_contents($api_url);
if(function_exists('gzdecode')){
$content = gzdecode($content);
} else {
$content = self::gzdecode($content);
}
return $content;
} public static function gzdecode($data) {
$len = strlen ( $data );
if ($len < 18 || strcmp ( substr ( $data, 0, 2 ), "\x1f\x8b" )) {
return null; // Not GZIP format (See RFC 1952)
}
$method = ord ( substr ( $data, 2, 1 ) ); // Compression method
$flags = ord ( substr ( $data, 3, 1 ) ); // Flags
if ($flags & 31 != $flags) {
// Reserved bits are set -- NOT ALLOWED by RFC 1952
return null;
}
// NOTE: $mtime may be negative (PHP integer limitations)
$mtime = unpack ( "V", substr ( $data, 4, 4 ) );
$mtime = $mtime [1];
$xfl = substr ( $data, 8, 1 );
$os = substr ( $data, 8, 1 );
$headerlen = 10;
$extralen = 0;
$extra = "";
if ($flags & 4) {
// 2-byte length prefixed EXTRA data in header
if ($len - $headerlen - 2 < 8) {
return false; // Invalid format
}
$extralen = unpack ( "v", substr ( $data, 8, 2 ) );
$extralen = $extralen [1];
if ($len - $headerlen - 2 - $extralen < 8) {
return false; // Invalid format
}
$extra = substr ( $data, 10, $extralen );
$headerlen += 2 + $extralen;
}
$filenamelen = 0;
$filename = "";
if ($flags & 8) {
// C-style string file NAME data in header
if ($len - $headerlen - 1 < 8) {
return false; // Invalid format
}
$filenamelen = strpos ( substr ( $data, 8 + $extralen ), chr ( 0 ) );
if ($filenamelen === false || $len - $headerlen - $filenamelen - 1 < 8) {
return false; // Invalid format
}
$filename = substr ( $data, $headerlen, $filenamelen );
$headerlen += $filenamelen + 1;
} $commentlen = 0;
$comment = "";
if ($flags & 16) {
// C-style string COMMENT data in header
if ($len - $headerlen - 1 < 8) {
return false; // Invalid format
}
$commentlen = strpos ( substr ( $data, 8 + $extralen + $filenamelen ), chr ( 0 ) );
if ($commentlen === false || $len - $headerlen - $commentlen - 1 < 8) {
return false; // Invalid header format
}
$comment = substr ( $data, $headerlen, $commentlen );
$headerlen += $commentlen + 1;
} $headercrc = "";
if ($flags & 1) {
// 2-bytes (lowest order) of CRC32 on header present
if ($len - $headerlen - 2 < 8) {
return false; // Invalid format
}
$calccrc = crc32 ( substr ( $data, 0, $headerlen ) ) & 0xffff;
$headercrc = unpack ( "v", substr ( $data, $headerlen, 2 ) );
$headercrc = $headercrc [1];
if ($headercrc != $calccrc) {
return false; // Bad header CRC
}
$headerlen += 2;
} // GZIP FOOTER - These be negative due to PHP's limitations
$datacrc = unpack ( "V", substr ( $data, - 8, 4 ) );
$datacrc = $datacrc [1];
$isize = unpack ( "V", substr ( $data, - 4 ) );
$isize = $isize [1]; // Perform the decompression:
$bodylen = $len - $headerlen - 8;
if ($bodylen < 1) {
// This should never happen - IMPLEMENTATION BUG!
return null;
}
$body = substr ( $data, $headerlen, $bodylen );
$data = "";
if ($bodylen > 0) {
switch ($method) {
case 8 :
// Currently the only supported compression method:
$data = gzinflate ( $body );
break;
default :
// Unknown compression method
return false;
}
} else { // I'm not sure if zero-byte body content is allowed.
// Allow it for now... Do nothing...
} // Verifiy decompressed size and CRC32:
// NOTE: This may fail with large data sizes depending on how
// PHP's integer limitations affect strlen() since $isize
// may be negative for large sizes.
if ($isize != strlen ( $data ) || crc32 ( $data ) != $datacrc) {
// Bad format! Length or CRC doesn't match!
return false;
}
return $data;
}
}
?>
使用起来非常简单,下面是一个调用程序:
<?php
/**
* demo.php
*
* 客户端调用示例
*
* @filename demo.php
* @version v1.0
* @update 2011-12-22
* @author homingway
* @contact homingway@gmail.com
* @package webservice
*/ include_once('../client/apiClient.php'); $server_uri = 'http://localhost/webservice/server/server.php'; print_r(apiClient::send($server_uri,'welcome.index'));
?>
一个PHP写的简单webservice服务端+客户端的更多相关文章
- eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(二)
eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(二) 接上篇博客,本篇博客主要包含两个内容: 4.使用Android studio创建webservice客 ...
- eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(一)
eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(一) 本篇博客主要包含五个内容: 1.CXF换将搭建以及eclipse配置CXF. 2.eclipse创建w ...
- 手写内网穿透服务端客户端(NAT穿透)原理及实现
Hello,I'm Shendi. 这天心血来潮,决定做一个内网穿透的软件. 用过花生壳等软件的就知道内网穿透是个啥,干嘛用的了. 我们如果有服务器(比如tomcat),实际上我们在电脑上开启了服务器 ...
- JAVA WEBSERVICE服务端&客户端的配置及调用(基于JDK)
前言:我之前是从事C#开发的,因公司项目目前转战JAVA&ANDROID开发,由于对JAVA的各种不了解,遇到的也是重重困难.目前在做WEBSERVICE提供数据支持,看了网上相关大片的资料也 ...
- AgileEAS.NET SOA 中间件平台.Net Socket通信框架-简单例子-实现简单的服务端客户端消息应答
一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...
- WebService 服务端客户端 实例 HTTPRIO (一) SOAP WSDL
Delphi中WebService包含的组件解释(有7个) (1) THTTPRIO-------:使用Http消息来调用远程使用SOAP的接口对象 (2) THTTPReqResp- ...
- DelphiXE7中创建WebService(服务端+客户端)
相关资料: http://www.2ccc.com/news/Html/?1507.html http://www.dfwlt.com/forum.php?mod=viewthread&tid ...
- DelphiXE7中创建WebService(服务端+客户端) good
相关资料:http://www.2ccc.com/news/Html/?1507.html DelphiXE7新建WebService具体操作:1.打开“DelphiXE7”->“File”-& ...
- PHP写webservice服务端
1) WebService技术介绍 WebService是一种跨编程语言和跨操作系统平台的远程调用技术.仅仅有通过Web Service,client和server才可以自由的用HTTP进行通信.不论 ...
随机推荐
- (原创)JS闭包看代码理解
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...
- JavaScript实现Ajax小结
置顶文章:<纯CSS打造银色MacBook Air(完整版)> 上一篇:<TCP的三次握手和四次挥手> 作者主页:myvin 博主QQ:851399101(点击QQ和博主发起临 ...
- [USACO2005][POJ3169]Layout(差分约束)
题目:http://poj.org/problem?id=3169 题意:给你一组不等式了,求满足的最小解 分析: 裸裸的差分约束. 总结一下差分约束: 1.“求最大值”:写成"<=& ...
- 第一课:js命名空间的介绍,js对象的扩展以及js数组化
1.命名空间: js里面的命名空间就是使用对象的属性来扩展的.比如,用户定义一个A对象,A对象下面有B属性和C属性,同时B属性和C属性又是对象.因此A={B:{},C:{}},这时用户就可以在B对象和 ...
- Moqui学习之数据与资源
资源位置: 资源门面位置的字符串类似于URL的构成方式:协议,主机,可选端口和文件名.它支持标准的java URL协议(http https ftp jar file).同样也支持一些扩展的协议: c ...
- PHP微信登錄(網頁授權)之後的獲取用戶的信息
//這部峯代碼是封裝的庫文件,<?php /** * Created by PhpStorm. * User: root * Date: 16-6-23 * Time: 下午3:29 */ cl ...
- 常用js正则
selectedName = selectedName.replace(/,$/, '');//去除尾部字符串 selectedCode = selectedCode.replace(/,$/, '' ...
- “耐撕”团队2016.05.11站立会议
1. 时间 18:15--18:35 共计20分钟 2. 人员 Z 郑蕊 * 组长 (博客:http://www.cnblogs.com/zhengrui0452/), P 濮成林(博客:ht ...
- bugzilla_firefox
//本来要给火狐提交bug的,发现复现不鸟,我勒个去 <!doctype html> <html> <head> <meta charset="ut ...
- Java-LinkedHashSet
如下: package 集合类.Set类; import java.util.Arrays; import java.util.HashSet; import java.util.LinkedHash ...