时间:2021-07-01 10:21:17 帮助过:30人阅读
Soapserver.php
Java代码
<?php
//先创建一个SoapServer对象实例,然后将我们要暴露的函数注册,
//最后的handle()用来处理接受的soap请求
error_reporting(7); //正式发布时,设为 0
date_default_timezone_set('PRC'); //设置时区
/* 几个供client端调用的函数 */
function reverse($str)
{
$retval = '';
if (strlen($str) < 1) {
return new SoapFault ('Client', '', 'Invalid string');
}
for ($i = 1; $i <= strlen($str); $i++) {
$retval .= $str [(strlen($str) - $i)];
}
return $retval;
}
function add2numbers($num1, $num2)
{
if (trim($num1) != intval($num1)) {
return new SoapFault ('Client', '', 'The first number is invalid');
}
if (trim($num2) != intval($num2)) {
return new SoapFault ('Client', '', 'The second number is invalid');
}
return ($num1 + $num2);
}
function gettime()
{
$time = date('Y-m-d H:i:s', time());
return $time;
}
$soap = new SoapServer (null, array('uri' => "httr://test-rui"));
$soap->addFunction('reverse');
$soap->addFunction('add2numbers');
$soap->addFunction('gettime');
$soap->addFunction(SOAP_FUNCTIONS_ALL);
$soap->handle();
?>
SoapClient.php
Java代码
<?php
error_reporting(7);
try {
$client = new SoapClient (null, array('location' => "http://www.yiigo.com/Soapserver.php", 'uri' => "http://test-uri"));
$str = "This string will be reversed";
$reversed = $client->reverse($str);
echo "if you reverse '$str', you will get '$reversed'";
$n1 = 20;
$n2 = 33;
$sum = $client->add2numbers($n1, $n2);
echo "<br>";
echo "if you try $n1 + $n2, you will get $sum";
echo "<br>";
echo "The remoye system time is: " . $client->gettime();
} catch (SoapFault $fault) {
echo "Fault! code:" . $fault->faultcode . " string:" . $fault->faultstring;
}
?>
if you reverse 'This string will be reversed', you will get 'desrever eb lliw gnirts sihT'
if you try 20 + 33, you will get 53
The remoye system time is: 2012-05-28 16:14:29
通过SoapHeader实现身份认证
Java代码
<?php
class Server
{
public function auth($a)
{
if ($a != '123456789') {
throw new SoapFault('Server', '用户身份认证信息错误');
}
}
public function say()
{
return 'Hi';
}
}
$srv = new SoapServer(null, array('uri' => 'http://localhost/namespace'));
$srv->setClass('Server');
$srv->handle();
客户端
Java代码
<?php
$cli = new SoapClient(null,
array('uri' => 'http://localhost/namespace/',
'location' => 'http://localhost/server.php',
'trace' => true));
//auth为服务端要处理的函数 12345689为参数
$h = new SoapHeader('http://localhost/namespace/',
'auth', '123456789', false, SOAP_ACTOR_NEXT);
$cli->__setSoapHeaders(array($h));
try {
echo $cli->say();
} catch (Exception $e) {
echo $e->getMessage();
}
注意观察server.php中的server类有一个方法“auth”,刚好与header的名称对应,方法auth的参数$u,就是soapHeader的data,soapServer接收到这个请求会,先调用auth方法,并把“123456789”作为参数传递给该方法。mustUnderstand参数为false时,即便没有auth这个方法,say方法也会被调用,但是如果它为true的话,如果auth方法不存在,就会返回一个Soapfault告知该header没有被处理。actor参数指名那些role必须处理该header,这儿我理解得不是太透彻,不好说。
Java代码
$file = $this->getSoapWSDL();
$client = new SoapClient($file);//url可以通过浏览器访问,不能直接调用解决
$param = array('userID' => 'test', 'merchantID' => 'test');
$returnSt = $client->checkUser($param);
print_r($returnSt->checkUserResult);
public function getSoapWSDL()
{ //定期将url的文件保存到本地
$file = Mage::getBaseDir() . DS . 'data' . DS . 'shengda' . DS . 'export.wsdl';
if (time() > filemtime($file) + 7 * 86400) {
$url = "http://jf.sdo.com/ExchangeScore/ExchangeService.asmx?WSDL";
include_once(BP . DS . "lib/Snoopy.class.php");
$snoopy = new Snoopy;
$snoopy->fetch($url); //获取所有内容
$snoopy->read_timeout = 4;
$wsdl = $snoopy->results;
if ($snoopy->status == '200' && !$snoopy->timed_out) {
if (!is_dir(dirname($file))) {
mkdir(dirname($file));
}
file_put_contents($file, $wsdl);
}
}
return $file;
}