利用Web
Service的 soap
协议--- 提供WSDL
接口。 可以实现Web各种的服务。
以下是一个测试类:
<?PHP
class test {
Public
function sum($a, $b) {
return $a + $b;
}
}
?>
服务器端:
<?php
require 'test.php';
$server = new SoapServer ( 'test.wsdl' );
$server->setClass ( 'test' );
$server->handle ();
?>
其中生成的WSDL:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost/webserver" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/
XMLSchema" name="test" targetNamespace="http://localhost/webserver">
<wsdl:message name="sumRequest">
<wsdl:part name="a" type="xsd:string"></wsdl:part>
<wsdl:part name="b" type="xsd:string"></wsdl:part>
</wsdl:message>
<wsdl:message name="sumResponse">
<wsdl:part name="return" type="xsd:string"/>
</wsdl:message>
<wsdl:portType name="test">
<wsdl:
operation name="sum">
<wsdl:input message="tns:sumRequest"/>
<wsdl:output message="tns:sumResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="testSOAP" type="tns:test">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sum">
<soap:operation soapAction="http://localhost/webserver/New
Operation"/>
<wsdl:input>
<soap:body namespace="http://localhost/webserver" use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body namespace="http://localhost/webserver" use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="test">
<wsdl:port binding="tns:testSOAP" name="testSOAP">
<soap:address location="http://localhost/webserver/server.php"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
用客户端测试 既为:
<?php
$soap = new SoapClient ( 'http://localhost/webserver/test.wsdl' );
print_r ( $soap->__getFunctions () );
echo $soap->sum(3,4);
?>
结果为 7 。 即可用java android c++ 来读取WSDL接口 从而实现其各种特定的方法。