一:PHP本身的SOAP
所有的webservice都包括服務(wù)端(server)和客戶端(client)。要使用php本身的soap首先要把該拓展安裝好并且啟用。下面看具體的code首先這是服務(wù)端實(shí)現(xiàn):PHP Code復(fù)制內(nèi)容到剪貼板
- <?php
- class test
- {
- function show()
- {
- return 'the data you request!';
- }
- }
- function getUserInfo($name)
- {
- return 'fbbin';
- }
- //實(shí)例化的參數(shù)手冊(cè)上面有,這個(gè)是沒有使用wsdl的,所以第一個(gè)參數(shù)為null,如果有使用wsdl,那么第一個(gè)參數(shù)就是這個(gè)wsdl文件的地址。
- $server = new SoapServer(null, array('uri' ='http://soap/','location'='http://localhost/test/server.php'));
- $server->setClass('test');
- //$server->addFunction('getUserInfo');
- $server->handle();
- ?>
然后是客戶端PHP Code復(fù)制內(nèi)容到剪貼板
- $soap = new SoapClient(null, array('location'='http://localhost/test/server.php','uri' ='http://soap/'));
-
- echo $soap->show();
- //得到:'the data you request!'
-
- //echo $soap->getUserInfo('sss');
就這么簡(jiǎn)單,當(dāng)時(shí)這只是一個(gè)很簡(jiǎn)單的例子,其實(shí)很多的通信機(jī)制都是這么去實(shí)現(xiàn)的!////////////////////////////////////////////////////////////////////////////////二:PHPRPC
首先到官網(wǎng)(http://www.phprpc.org/zh_CN/ )上面去下載最新版的phprpc,解壓之后會(huì)有相關(guān)的文件,我們把文件進(jìn)行劃分(服務(wù)端和客戶端文件)如下:服務(wù)端文件:PHP Code復(fù)制內(nèi)容到剪貼板
- dhparams
- dhparams.php
- phprpc_server.php
- bigint.php
- compat.php
- phprpc_date.php
- xxtea.php
客戶端文件:PHP Code復(fù)制內(nèi)容到剪貼板
- phprpc_client.php
- bigint.php
- compat.php
- phprpc_date.php
- xxtea.php
我們把服務(wù)端文件放在服務(wù)端文件夾中,然后把客戶端文件放在客戶端文件夾中,之后再服務(wù)端文件夾中新建個(gè)文件(server.php)作為服務(wù),然后再客戶端新建個(gè)文件(client.php)作為客戶端,各自代碼如下:server端:PHP Code復(fù)制內(nèi)容到剪貼板
- <?php
- include_once"phprpc_server.php"; //加載phprpc文件
- $server = new PHPRPC_Server();
- $server->add('getUser');
- $server->setDebugMode(true);
- $server->start();
- function getUser( )
- {
- return ‘the data you request!’;
- }
-
- client端:
- [code]
- <?php
- include_once "phprpc_client.php";
- $client = new PHPRPC_Client('http://127.0.0.1/phpservice/phprpcserver/server.php');
- $data = $client->getUser();
- var_dump($data);
- //得到:the data you request!
這上面提到wsdl之后會(huì)講到如何生成。////////////////////////////////////////////////////////////////////////////////三:開源的NUSOAP
首先到網(wǎng)上去下載最新版的nusoap,現(xiàn)在的最新版本是0.9.5的,解壓之后會(huì)得到一個(gè)lib文件夾,把這個(gè)文件分別放到服務(wù)端和客戶端各一份,然后再服務(wù)端和客戶端分別建立server.php和client.php文件,作為通信文件。服務(wù)端文件如下:PHP Code復(fù)制內(nèi)容到剪貼板
- <?php
- ini_set("soap.wsdl_cache_enabled", 0);//關(guān)閉緩存
- require_once("lib/nusoap.php"); //加載nusoap文件
- $server = new soap_server;
- $server->configureWSDL('nusoasp');//設(shè)定服務(wù)的名稱,使用的wsdl來通信,如果不適用wsdl將會(huì)更簡(jiǎn)單,網(wǎng)上有很多的例子
- $server->register('getUserInfo', array('name'="xsd:string", 'email'="xsd:string"), array('return'="xsd:string"));
- $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
- $server->service( $HTTP_RAW_POST_DATA );
- function getUserInfo($name, $email)
- {
- return ‘the data you request!’;
- }
客戶端文件如下:PHP Code復(fù)制內(nèi)容到剪貼板
- require_once("lib/nusoap.php");
- $client = new soapclient('http://localhost/phpservice/nusoapserver/server.php?wsdl');
- $pagram = array('fbbin', 'fbbin@foxmail.com');
- $string = $client->call('getUserInfo', $pagram);
- //得到:the data you request!
///////////////////////////////////////////////////////////////////////////////四:HessianPHP
hessian其實(shí)我個(gè)人認(rèn)為他不是一個(gè)webservice,只能說是類似而已。因?yàn)樗痪邆鋡ebservice的那些特性。它支持的語(yǔ)言比較多我們現(xiàn)在只需要研究php版本的HessianPHP就行了,下載最新版本是v2.0.3的,解壓之后會(huì)得到一個(gè)src的目錄,這個(gè)目錄使我們需要使用的一個(gè)核心文件夾。我們把名字重命名為HessianPHP然后分別分別放到server和client端,然后分別建立server.php和client.php文件。server端:PHP Code復(fù)制內(nèi)容到剪貼板
- <?php
- include_once 'HessianPHP/HessianService.php';//加載核心文件
- class TestService
- {
- public function __construct()
- {
-
- }
-
- public function add($numa, $numb)
- {
- return $numa + $numb;
- }
-
- public function check()
- {
- return 'fbbiin@gmail.com';
- }
- }
- $test = new TestService();
- $hessian = new HessianService( $test, array('displayInfo' => true) );
- $hessian->handle();//注意這里不是網(wǎng)上的$hessian->service(),可能是版本不一樣,改了吧!我也是看了源碼才知道!
- ?>
client 端:PHP Code復(fù)制內(nèi)容到剪貼板