php的rpc扩展之soap

本文介绍如何在 PHP 中使用 SOAP 协议实现服务端与客户端的数据交互。通过一个简单的例子,展示了如何搭建 SOAP 服务端及调用服务端的方法。
SOAP 是基于 XML 的简易协议,可使应用程序在 HTTP 之上进行信息交换。

要想在php中使用soap,需要在编译php时,指定configure参数--enable-soap

一个简单的soap case:

soap的server端代码 server.php:

[php]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. class MyClass {  
  3.   public function helloWorld() {  
  4.     return 'Hallo Welt '. print_r(func_get_args(), true);  
  5.   }  
  6. }  
  7.   
  8. try {  
  9.   $server = new SOAPServer(  
  10.     NULL,  
  11.     array(  
  12.      'uri' => 'http://soap.yesuhuangsi.com/soap/server.php'  
  13.     )  
  14.   );  
  15.   
  16.   $server->setClass('MyClass');  
  17.   $server->handle();  
  18. }  
  19. catch (SOAPFault $f) {  
  20.   print $f->faultstring;  
  21. }  

将其放在项目的相应目录下,以便可以通过对应的uri访问。


soap的client端代码 client.php

client的代码可以是其他项目甚至在其他服务器上。

[php]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2. $client = new SoapClient(null, array(  
  3.       'location' => "http://soap.yesuhuangsi.com/soap/server.php",  
  4.       'uri'      => "http://soap.yesuhuangsi.com/soap/server.php",  
  5.       'trace'    => 1 ));  
  6.   
  7. echo $return = $client->__soapCall("helloWorld",array("world"));  

执行client.php输出:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Hallo Welt Array  
  2. (  
  3.     [0] => world  
  4. )  

需要注意的是,在更改了server.php代码之后,需要重启php-fpm才会生效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值