共三个文件,测试有效(需开soap扩展)
soapfun.php
<?php</div>
function reverse($str){</div>
$retval = '';</div>
if(strlen($str) < 1) {</div>
<div id="_mcePaste">return new SoapFault('Client','','Invalid string');</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">for ($i = 1; $i <= strlen($str); $i++) {</div>
<div id="_mcePaste">$retval .= $str[(strlen($str) - $i)];</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">return $retval;</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">function add2numbers($num1, $num2) {</div>
<div id="_mcePaste">if (trim($num1) != intval($num1)) {</div>
<div id="_mcePaste">return new SoapFault('Client','','The first number is invalid');</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">if (trim($num2) != intval($num2)) {</div>
<div id="_mcePaste">return new SoapFault('Client','','The second number is invalid');</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">return ($num1 + $num2);</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">function gettime(){</div>
<div id="_mcePaste">$time=strftime("%Y-%m-%d %H:%M:%S");</div>
<div id="_mcePaste">return $time;</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">?></div>
soapser.php
<?
include_once('soapfun.php');
$soap = new SoapServer(null,array('uri'=>"http://testq-uri/"));
$soap->addFunction('reverse');
$soap->addFunction('add2numbers');
$soap->addFunction('gettime');
$soap->addFunction(SOAP_FUNCTIONS_ALL);
$soap->handle();
?></pre>
<div></div>
<pre>
soapcli.php
</pre>
</span>
<pre style="font: normal normal normal 12px/18px Consolas, Monaco, 'Courier New', Courier, monospace;"><?
try {
$client = new SoapClient(null, array('location' =>
"http://localhost/test/soapser.php",'uri' => "http://test-uri/"));
$str = "This string will be reversed";
$reversed = $client->reverse($str);
echo "If you reverse '",$str,"', you get '",$reversed,"'";
echo "<br>";
$n1=20;
$n2=33;
$sum = $client->add2numbers($n1,$n2);
echo "If you try ",$n1,"+",$n2,", you will get ",$sum,"";
echo "<br>";
echo "The system time is: ",$client->gettime();
} catch (SoapFault $fault){
echo "Fault! code:",$fault->faultcode,", string: ",$fault->faultstring;
}
?></pre>
<span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: 19px; white-space: normal;">
<pre style="font: normal normal normal 12px/18px Consolas, Monaco, 'Courier New', Courier, monospace;">