php的rpc扩展之soap

本文介绍如何在 PHP 中使用 SOAP 协议实现服务端与客户端的数据交互。通过一个简单的例子,展示了如何搭建 SOAP 服务端及调用服务端的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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才会生效。
JsonRPC 2.0 Client and Server ============================= 轻量级 Json-RPC 2.0 客户端和服务端的php扩展,基于 multi_curl epoll的并发客户端,依据[jsonrpc](http://www.jsonrpc.org/)协议规范。 服务端: $server = new Jsonrpc_Server(); // style one function variable $add1 = function($a, $b){     return $a   $b; }; $server->register('addition1', $add1); // style two function string function add2($a, $b){   return $a   $b; } $server->register('addition2', 'add2'); // style three function closure $server->register('addition3', function ($a, $b) {     return $a   $b; }); //style four class method string class A  {   static public function add($a, $b)   {     return $a   $b;   } } $server->register('addition4', 'A::add'); echo $server->execute(); //output >>> //{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error"}} 客户端: $client = new Jsonrpc_Client(1); $client->call('http://localhost/server.php', 'addition1', array(3,5)); $client->call('http://localhost/server.php', 'addition2', array(10,20)); $client->call('http://localhost/server.php', 'addition3', array(2,8)); $client->call('http://localhost/server.php', 'addition4', array(6,15)); /* ... */ $result = $client->execute(); var_dump($result); //output >>> /* array(2) {   [0]=>   array(3) {     ["jsonrpc"]=>     string(3) "2.0"     ["id"]=>     int(110507766)     ["result"]=>     int(8)   }   [1]=>   array(3) {     ["jsonrpc"]=>     string(3) "2.0"     ["id"]=>     int(1559316299)     ["result"]=>     int(30)   }   ... } */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值