我尝试创建一个简单的Web服务。一切正常,但我在wsdl中定义的限制不起作用。
我尝试了两种类型的限制:minLength / maxLength和枚举。我从客户端发送的数据无关紧要,一切都被接受......
以下是我的代码:
WSDL:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:tns="http://localhost/APItest"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns="http://schemas.xmlsoap.org/wsdl/"
targetNamespace="http://localhost/APItest">
<wsdl:types>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
targetNamespace="http://localhost/APItest">
<xsd:element name="HelloRequest">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:minLength value="5"/>
<xsd:maxLength value="8"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="car">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Audi"/>
<xsd:enumeration value="Golf"/>
<xsd:enumeration value="BMW"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="HelloResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" name="result" type="xsd:string"/>
<xsd:element minOccurs="0" name="time" type="xsd:dateTime"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="Hello">
<wsdl:part name="input" element="tns:HelloRequest"/>
</wsdl:message>
<wsdl:message name="HelloResponse">
<wsdl:part name="output" element="tns:HelloResponse"/>
</wsdl:message>
<wsdl:portType name="APIPortType">
<wsdl:operation name="Hello">
<wsdl:input message="tns:Hello"/>
<wsdl:output message="tns:HelloResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="APIHttpBinding" type="tns:APIPortType">
<http:binding verb="POST"/>
<wsdl:operation name="Hello">
<http:operation location="API/Hello"/>
<wsdl:input>
<mime:content part="Hello" type="text/xml"/>
</wsdl:input>
<wsdl:output>
<mime:content part="Hello" type="text/xml"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="API">
<wsdl:port name="APIHttpport" binding="tns:APIHttpBinding">
<http:address location="http://localhost/APItest/server.php"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
server.php:
<?php
error_reporting(-1);
ini_set('display_errors', TRUE);
// Disable WSDL Cache
ini_set('soap.wsdl_cache_enabled', 0);
ini_set('soap.wsdl_cache_ttl', 0);
class API
{
public function Hello($params)
{
$response = new stdClass();
$response->result = "Hello " . $params->name . ", your car is '" . $params->car . "'!";
$response->time = date("Y-m-d H:i:s");
file_put_contents("logs/log_" . date("Ymd-His") . "_ERROR.txt", print_r($this, true));
return $response;
}
}
//create a new SOAP server
$server = new SoapServer("API.wsdl");
//attach the API class to the SOAP Server
$server->setClass('API');
//start the SOAP requests handler
$server->handle();
client.php:
<?php
error_reporting(-1);
ini_set('display_errors', TRUE);
// Disable WSDL Cache
ini_set('soap.wsdl_cache_enabled', 0);
ini_set('soap.wsdl_cache_ttl', 0);
$wsdl = "API.wsdl";
$options = array(
'trace' => true,
'encoding' => 'ISO-8859-1',
'soap_version' => SOAP_1_2
);
$soapClient = new SoapClient($wsdl, $options);
$input = new stdClass();
$input->name = "MartinMartinMartinMartin";
$input->car = "Ford";
try {
$hello = $soapClient->Hello($input);
echo '<pre>' . print_r($hello, true) . '</pre>';
} catch (Exception $e) {
echo "<h3>Error calling last Request!</h3>";
echo '<pre>' . print_r($e, true) . '</pre>';
}
你知道出了什么问题吗?