|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-03-09 18:19 UTC] kevin dot craft at gmail dot com
Description:
------------
When using a SoapServer object in WSDL mode, PHP DateTime objects are converted to an empty xsd:datetime element. To produce the correct results, DateTime objects have to be formatted as strings, which defeats the purpose of using the SoapServer to map data types.
Environment:
------------
O/S: Windows XP
Web Server: Apache 2.2
PHP: 5.2.5 w/ GD, MySQL, and SOAP
Reproduce code:
---------------
bug.wsdl:
<?xml version ="1.0" encoding ="UTF-8" ?>
<wsdl:definitions name="GetCurrentDate"
targetNamespace="http://localhost"
xmlns:tns="http://localhost"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:message name="GetCurrentDateRequest" />
<wsdl:message name="GetCurrentDateResponse">
<wsdl:part name="currentDate" type="xsd:datetime" />
</wsdl:message>
<wsdl:portType name="GetCurrentDatePortType">
<wsdl:operation name="getCurrentDate">
<wsdl:input message="tns:GetCurrentDateRequest" />
<wsdl:output message="tns:GetCurrentDateResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="GetCurrentDateBinding" type="tns:GetCurrentDatePortType">
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="getCurrentDate">
<soap:operation soapAction="http://localhost#getCurrentDate" />
<wsdl:input>
<soap:body use="encoded"
namespace="urn:xmethods-delayed-quotes"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</wsdl:input>
<wsdl:output>
<soap:body use="encoded"
namespace="urn:xmethods-delayed-quotes"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="GetCurrentDateService">
<wsdl:port name="GetCurrentDatePortType" binding="GetCurrentDateBinding">
<soap:address location="http://localhost" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
server.php:
<?php
function getCurrentDate() {
return new DateTime();
}
$server = new SoapServer('bug.wsdl');
$server->addFunction('getCurrentDate');
$server->handle();
?>
client.php
<?php
$client = new SoapClient('bug.wsdl', array('trace' => 1));
echo 'current date: '.$client->getCurrentDate().'<br /><br />';
echo 'last response: '.htmlentities($client->__getLastResponse());
?>
Expected result:
----------------
Assuming the current date is 2008-03-08 23:00:00:
current date: 2008-03-06 00:00:00 (or any other format accepted by xsd:datetime)
last response: <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:xmethods-delayed-quotes" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:getCurrentDateResponse><currentDate xsi:type="xsd:datetime">2008-03-06 00:00:00</currentDate></ns1:getCurrentDateResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
Actual result:
--------------
current date:
last response: <?xml version="1.0" encoding="UTF-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:xmethods-delayed-quotes" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:getCurrentDateResponse><currentDate xsi:type="xsd:datetime" /></ns1:getCurrentDateResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 02:00:01 2025 UTC |
For me, when you call $client->getCurrentDate(), the expected result would be to have a PHP DateTime object rather than a string. So the expected result should be: DateTime Object ( [date] => 2008-03-06 00:00:00 [timezone_type] => x [timezone] => xxxx/xxxxx ) What do you think about this? At least an option could be provide throught the SoapClient to permit this behavior.The Bug still exist to this day in PHP 8.2+ my current workaround is using the typemap option for SOAP Client: protected static array $typeMap = [ [ "type_ns" => "h**p://www.w3.org/2001/XMLSchema", "type_name" => "date", "to_xml" => [self::class, 'toXSDDate'] ] ]; /** * SOAP Bug with Datetime https://bugs.php.net/bug.php?id=44383 * * @param DateTimeInterface|null $dateTime * @return string|null */ public static function toXSDDate(?DateTimeInterface $dateTime): ?string { if (is_null($dateTime)) { return null; } return $dateTime->format(DateTimeInterface::ATOM); }because it is nowhere documented, but typemap to_xml needs a valid xml string, otherwise it will silently do nothing there is my fixed example function toXSDDate(?DateTimeInterface $dateTime) : ?string { return '<a>' . $dateTime->format(DateTimeInterface::ATOM) . '</a>'; }