原文: https://www.jianshu.com/p/16b039956cf9
下面,在windows宿主机上面(192.168.123.149上面,访问其webroot目录下面的client2.php)
<?php try { $client = new SOAPClient(null, [ ‘location‘ => ‘http://192.168.123.41:888/server2.php‘, ‘uri‘ => ‘http://192.168.123.41:888/server2.php‘, ]); // 直接调用 echo $client->strtolink(‘192.168.123.41:888‘) . ‘<br>‘; // 间接调用 // echo $client->__soapCall(‘strtolink‘, [‘192.168.123.41:888‘]); } catch (SOAPFault $e) { echo $e->getMessage(); }
tcpdump到192.168.123.41上面抓包
tcpdump tcp port 888 -w soap.pcap
--------------------------------------------------------------------------------------------------------------------
SOAP 简单对象访问协议是交换数据的一种协议规范,是一种轻量的、简单的、基于XML(标准通用标记语言下的一个子集)的协议,它被设计成在WEB上交换结构化的和固化的信息。
SOAP(Simple Object Access Protocol )简单对象访问协议是在分散或分布式的环境中交换信息的简单的协议,是一个基于XML的协议,它包括四个部分:
优点
SOAP有两种操作方式,NO-WSDL 与 WSDL
NO-WSDL模式:使用参数来传递要使用的信息
WSDL模式: 使用WSDL文件名作为参数,并从WSDL中提取服务所需的信息。(WSDL文件结构在系统迭代频繁的时候,没有NO-WSDL模式灵活,这里暂时不讲.)
SOAP中主要用到三个类,SOAPServer,SOAPClient,SOAPFault.
简单示例:
server.php
<?php
class soapHandle
{
public function strtolink($url = ‘‘) {
return sprintf(‘<a href="%s">%s</a>‘, $url, $url);
}
}
try {
$server = new SOAPServer(null, [‘uri‘ => ‘http://127.0.0.1:8002/demo/soap/NO-WSDL/server.php‘]);
$server->setClass(‘soapHandle‘);
$server->handle();
} catch (SOAPFault $f) {
echo $f->getMessage();
}
client.php
<?php
try {
$client = new SOAPClient(null, [
‘location‘ => ‘http://127.0.0.1:8002/demo/soap/NO-WSDL/server.php‘,
‘uri‘ => ‘http://127.0.0.1:8002/demo/soap/NO-WSDL/server.php‘,
]);
// 直接调用
echo $client->strtolink(‘www.demo.com‘) . ‘<br>‘;
// 间接调用
echo $client->__soapCall(‘strtolink‘, [‘www.demo.com‘]);
} catch (SOAPFault $e) {
echo $e->getMessage();
}
简单示例升级
server.php
<?php
class SOAPHandle
{
public function auth($auth) {
if ($auth->string[0] != ‘demo‘ || $auth->string[1] != ‘123456‘) {
throw new SOAPFault(‘Server‘, ‘No Permission‘);
}
}
public function strtolink($str = ‘‘, $name = ‘‘, $openwin = 0) {
$name = $name == ‘‘ ? $str : $name;
$openwin_tag = $openwin == 1 ? ‘ target="_blank" ‘ : ‘‘;
return sprintf(‘<a href="%s" %s>%s</a>‘, $str, $openwin_tag, $name);
}
}
$config = array(
‘uri‘ => ‘http://127.0.0.1:8002/demo/soap/NO-WSDL-Header/server.php‘
);
$objHandle = new SOAPHandle;
// no wsdl mode
try {
$server = new SOAPServer(null, $config);
$server->setObject($objHandle );
$server->handle();
} catch (SOAPFault $f) {
echo $f->faultString;
}
client.php
<?php
$config = [
‘location‘ => ‘http://127.0.0.1:8002/demo/soap/NO-WSDL-Header/server.php‘,
‘uri‘ => ‘http://127.0.0.1:8002/demo/soap/NO-WSDL-Header/server.php‘,
‘trace‘ => true
];
try {
$auth = [
‘demo‘,
‘123456‘
];
// no wsdl
$client = new SOAPClient(null, $config);
/*
* SoapHeader参数说明如下所示:
* ‘http://tempuri.org/‘ namespace(命名空间可省略)
* ‘MySoapHeader‘ SoapHeader头的类名
* ‘array(...)‘ 存放标识身份的字符串参数
* ‘true‘ 是否必须处理该header
*/
$header = new SOAPHeader(‘http://127.0.0.1:8002/demo/soap/NO-WSDL-Header/server.php‘, ‘auth‘, $auth, false, SOAP_ACTOR_NEXT);
$client->__setSoapHeaders([$header]);
$strtolink = $client->__soapCall(‘strtolink‘, [
‘http://www.demo.com‘,
‘测试网址‘,
1
]);
echo $strtolink . ‘<br>‘;
} catch (SOAPFault $e) {
echo $e->getMessage();
}
参考
浅谈 SOAP
作者:谁不曾年少轻狂过
链接:https://www.jianshu.com/p/16b039956cf9
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
原文:https://www.cnblogs.com/oxspirt/p/12836360.html