SOAP 是基于 XML 的简易协议,可使应用程序在 HTTP 之上进行信息交换。
要想在php中使用soap,需要在编译php时,指定configure参数--enable-soap。
一个简单的soap case:
soap的server端代码 server.php:
<?php
class MyClass {
public function helloWorld() {
return 'Hallo Welt '. print_r(func_get_args(), true);
}
}
try {
$server = new SOAPServer(
NULL,
array(
'uri' => 'http://soap.yesuhuangsi.com/soap/server.php'
)
);
$server->setClass('MyClass');
$server->handle();
}
catch (SOAPFault $f) {
print $f->faultstring;
}
将其放在项目的相应目录下,以便可以通过对应的uri访问。
soap的client端代码 client.php
client的代码可以是其他项目甚至在其他服务器上。
<?php
$client = new SoapClient(null, array(
'location' => "http://soap.yesuhuangsi.com/soap/server.php",
'uri' => "http://soap.yesuhuangsi.com/soap/server.php",
'trace' => 1 ));
echo $return = $client->__soapCall("helloWorld",array("world"));
执行client.php输出:
Hallo Welt Array
(
[0] => world
)
需要注意的是,在更改了server.php代码之后,需要重启php-fpm才会生效。