SOAP SWLD 模式:
服务端:
server.php
class testA
{
function greet($param)
{
$value = 'Hello ' . $param->name;
$result = [
'greetReturn' => $value
];
return $result;
}
}
$server = new \SoapServer('wsdl.wsdl', array('trace' => 1));
$server->setClass('testA');
$server->handle();
// php -S 192.168.0.242:12312 server.php 可以使用PHP内置服务器,开启监听服务
客户端:
client.php
$client = new \SoapClient('wsdl.wsdl', array('trace' => 1));
//调用方法
$result = $client->__call('greet', [
[
'name' => 'Suhua'
]
]);
var_dump($result);
wsdl:
<wsdl:definitions
xmlns:impl='http://localhost/php-soap/wsdl/helloService'
xmlns:intf='http://localhost/php-soap/wsdl/helloService'
xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
xmlns:wsdlsoap='http://schemas.xmlsoap.org/wsdl/soap/'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
targetNamespace='http://localhost/php-soap/wsdl/helloService'>
<wsdl:types>
<schema elementFormDefault='qualified'
xmlns:impl='http://localhost/php-soap/wsdl/helloService'
xmlns:intf='http://localhost/php-soap/wsdl/helloService'
xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'
xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace='http://localhost/php-soap/wsdl/helloService' >
<element name='greet'>
<complexType>
<sequence>
<element name='name' type='xsd:string' />
</sequence>
</complexType>
</element>
<element name='greetResponse'>
<complexType>
<sequence>
<element name='greetReturn' type='xsd:string' />
</sequence>
</complexType>
</element>
</schema>
</wsdl:types>
<wsdl:message name='greetRequest'>
<wsdl:part name='parameters' element='impl:greet' />
</wsdl:message>
<wsdl:message name='greetResponse'>
<wsdl:part name='parameters' element='impl:greetResponse' />
</wsdl:message>
<wsdl:portType name='helloService'>
<wsdl:operation name='greet'>
<wsdl:input name='greetRequest' message='impl:greetRequest' />
<wsdl:output name='greetResponse' message='impl:greetResponse' />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name='helloServiceSoapBinding' type='impl:helloService'>
<wsdlsoap:binding transport='http://schemas.xmlsoap.org/soap/http' style='document' />
<wsdl:operation name='greet'>
<wsdlsoap:operation soapAction='helloService#greet' />
<wsdl:input name='greetRequest'>
<wsdlsoap:body use='literal' />
</wsdl:input>
<wsdl:output name='greetResponse'>
<wsdlsoap:body use='literal' />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name='helloService'>
<wsdl:port binding='impl:helloServiceSoapBinding' name='helloService'>
<wsdlsoap:address location='http://192.168.0.242:12312' />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
NON-WSDL 模式
服务端:
server.php
class testA
{
public function sayHi($str)
{
return 'hi,' . $str;
}
public function add($a, $b)
{
return $a + $b;
}
}
$ss = new \SoapServer(null, array('uri' => 'sampleA'));
$ss->setClass('testA');
$ss->handle();
客户端:
client:
$client = new \SoapClient(null, array(
'location'=>'http://192.168.0.242:12312',
'uri'=>'sampleA'
));
echo $client->sayHi('Taylor,Swift');
echo "<br/>";
echo $client->add(1,2);
小提示:
- 服务端可以使用php内置的服务尝试运行例子,php -S 192.168.0.242:12312 server.php
- trace 跟踪模式尽量开启,不然的话 __getLastRequest 这等函数获取不到数据
- 增加扩展后,默认WSDL会缓存,调试情况下关闭,避免很多奇怪问题 php.ini -> soap.wsdl_cache_enabled=0 默认 1 开启, 0 关闭
- 如果WSDL客户端调用的时候没有响应值的话应该是WSDL文件里不符合要求限制了
- NON-SWLD 模式下 location 和 uri 应和服务器保持一致,要不然会报错
- 通过学习认为这是一种RPC的模式
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。