使用 PHP 的 SOAP 扩展来创建 Web Service
SOAP有两种操作方式,NO-WSDL 与 WSDL。
NO-WSDL模式:使用参数来传递要使用的信息。
创建server.php
<?php
Class SiteInfo
{
public function getName(){
return "runoob";
}
public function getUrl(){
return "www.runoob.com";
}
}
// 创建 SoapServer 对象
$s = new SoapServer(null,array("location"=>"http://www.testest.com/soapa/server.php","uri"=>"server.php"));
// 导出 SiteInfo 类中的全部函数
$s->setClass("SiteInfo");
// 处理一个SOAP请求,调用必要的功能,并发送回一个响应。
$s->handle();
?>
创建client.php
<?php
$soap = new SoapClient(null,array('location'=>"http://www.testest.com/soapa/server.php",'uri'=>'server.php'));
// 调用函数
$result1 = $soap->getName();
$result2 = $soap->__soapCall("getUrl",array());
echo $result1."<br/>";
echo $result2;
WSDL模式: 使用WSDL文件名作为参数,并从WSDL中提取服务所需的信息。
创建SoapDiscovery.class.php
<?php
class SoapDiscovery {
private $class_name = '';
private $service_name = '';
public function __construct($class_name = '', $service_name = '') {
$this->class_name = $class_name;
$this->service_name = $service_name;
}
public function getWSDL() {
if (empty($this->service_name)) {
throw new Exception('No service name.');
}
$headerWSDL = "<?xml version=\"1.0\" ?>\n";
$headerWSDL.= "<definitions name=\"$this->service_name\" targetNamespace=\"urn:$this->service_name\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:tns=\"urn:$this->service_name\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns=\"http://schemas.xmlsoap.org/wsdl/\">\n";
$headerWSDL.= "<types xmlns=\"http://schemas.xmlsoap.org/wsdl/\" />\n";
if (empty($this->class_name)) {
throw new Exception('No class name.');
}
$class = new ReflectionClass($this->class_name);
if (!$class->isInstantiable()) {
throw new Exception('Class is not instantiable.');
}
$methods = $class->getMethods();
$portTypeWSDL = '<portType name="' . $this->service_name . 'Port">';
$bindingWSDL = '<binding name="' . $this->service_name . 'Binding" type="tns:' . $this->service_name . "Port\">\n<soap:binding style=\"rpc\" transport=\"http://schemas.xmlsoap.org/soap/http\" />\n";
$serviceWSDL = '<service name="' . $this->service_name . "\">\n<documentation />\n<port name=\"" . $this->service_name . 'Port" binding="tns:' . $this->service_name . "Binding\"><soap:address location=\"http://" . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['PHP_SELF'] . "\" />\n</port>\n</service>\n";
$messageWSDL = '';
foreach ($methods as $method) {
if ($method->isPublic() && !$method->isConstructor()) {
$portTypeWSDL.= '<operation name="' . $method->getName() . "\">\n" . '<input message="tns:' . $method->getName() . "Request\" />\n<output message=\"tns:" . $method->getName() . "Response\" />\n</operation>\n";
$bindingWSDL.= '<operation name="' . $method->getName() . "\">\n" . '<soap:operation soapAction="urn:' . $this->service_name . '#' . $this->class_name . '#' . $method->getName() . "\" />\n<input><soap:body use=\"encoded\" namespace=\"urn:$this->service_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n</input>\n<output>\n<soap:body use=\"encoded\" namespace=\"urn:$this->service_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n</output>\n</operation>\n";
$messageWSDL.= '<message name="' . $method->getName() . "Request\">\n";
$parameters = $method->getParameters();
foreach ($parameters as $parameter) {
$messageWSDL.= '<part name="' . $parameter->getName() . "\" type=\"xsd:string\" />\n";
}
$messageWSDL.= "</message>\n";
$messageWSDL.= '<message name="' . $method->getName() . "Response\">\n";
$messageWSDL.= '<part name="' . $method->getName() . "\" type=\"xsd:string\" />\n";
$messageWSDL.= "</message>\n";
}
}
$portTypeWSDL.= "</portType>\n";
$bindingWSDL.= "</binding>\n";
//return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>');
//生成wsdl文件,将上面的return注释
$fso = fopen($this->class_name . ".wsdl", "w");
fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>'));
}
public function getDiscovery() {
return "<?xml version=\"1.0\" ?>\n<disco:discovery xmlns:disco=\"http://schemas.xmlsoap.org/disco/\" xmlns:scl=\"http://schemas.xmlsoap.org/disco/scl/\">\n<scl:contractRef ref=\"http://" . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['PHP_SELF'] . "?wsdl\" />\n</disco:discovery>";
}
}
创建server.php
<?php
include 'SoapDiscovery.class.php';
class Service
{
public function Add($a,$b)
{
return $a+$b;
}
}
$soap = new SoapDiscovery('Service','servicename');
$soap->getWSDL();
//client请求
$server=new SoapServer('http://www.testest.com/soaptest/Service.wsdl');
$server->setClass("Service");
$server->handle();
生成Service.wsdl文件
<?xml version="1.0" ?>
<definitions name="servicename" targetNamespace="urn:servicename" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="urn:servicename" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns="http://schemas.xmlsoap.org/wsdl/">
<types xmlns="http://schemas.xmlsoap.org/wsdl/" />
<portType name="servicenamePort"><operation name="Add">
<input message="tns:AddRequest" />
<output message="tns:AddResponse" />
</operation>
</portType>
<binding name="servicenameBinding" type="tns:servicenamePort">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="Add">
<soap:operation soapAction="urn:servicename#Service#Add" />
<input><soap:body use="encoded" namespace="urn:servicename" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body use="encoded" namespace="urn:servicename" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
</binding>
<service name="servicename">
<documentation />
<port name="servicenamePort" binding="tns:servicenameBinding"><soap:address location="http://www.testest.com:80/soaptest/server.php" />
</port>
</service>
<message name="AddRequest">
<part name="a" type="xsd:string" />
<part name="b" type="xsd:string" />
</message>
<message name="AddResponse">
<part name="Add" type="xsd:string" />
</message>
</definitions>
创建client.php
<?php $soap = new SoapClient('http://www.testest.com/soaptest/Service.wsdl'); $res = $soap->Add(1,2); print_r($res);