Sto cercando di implementare un server SOAP utilizzando la classe Zend_Soap_Server
in PHP.Gli spazi dei nomi di PHP5 generano un nome di tipo WSDL errato durante l'implementazione di un servizio web
Ecco il file webservice.php
che è il punto di ingresso di richiesta:
<?php
require_once 'library.php';
require_once 'Zend/Loader/Autoloader.php';
$autoloader = \Zend_Loader_Autoloader::getInstance();
class Math
{
/**
* This method takes ...
*
* @param integer $inputParam
* @return \Library\IncrementedInt
*/
public function increment($inputParam)
{
return new \Library\IncrementedInt($inputParam);
}
}
$options = array('uri' => 'http://localhost' . $_SERVER['REQUEST_URI']);
if (isset($_GET['wsdl'])){
$server = new Zend_Soap_AutoDiscover();
$server->setClass('Math');
}
else {
$server = new Zend_Soap_Server(null, $options);
$server->setClass('Math');
$server->setObject(new Math());
}
$server->handle();
e ho library.php
file in questo modo:
<?php
namespace Library;
class IncrementedInt
{
public $original;
public $incremented;
public function __construct($num)
{
$this->original = $num;
$this->incremented = ++$num;
}
}
Una chiamata a http://localhost/webservice.php?wsdl
uscita volontà:
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://localhost/webservice.php" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" name="Math" targetNamespace="http://localhost/webservice.php">
<script/>
<types>
<xsd:schema targetNamespace="http://localhost/webservice.php">
<xsd:complexType name="\Library\IncrementedInt">
<xsd:all/>
</xsd:complexType>
</xsd:schema>
</types>
<portType name="MathPort">
<operation name="increment">
<documentation>This method takes ...</documentation>
<input message="tns:incrementIn"/>
<output message="tns:incrementOut"/>
</operation>
</portType>
<binding name="MathBinding" type="tns:MathPort">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="increment">
<soap:operation soapAction="http://localhost/webservice.php#increment"/>
<input>
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost/webservice.php"/>
</input>
<output>
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost/webservice.php"/>
</output>
</operation>
</binding>
<service name="MathService">
<port name="MathPort" binding="tns:MathBinding">
<soap:address location="http://localhost/webservice.php"/>
</port>
</service>
<message name="incrementIn">
<part name="inputParam" type="xsd:int"/>
</message>
<message name="incrementOut">
<part name="return" type="tns:\Library\IncrementedInt"/>
</message>
</definitions>
Ora per testare la funzionalità Io uso soapUI 4.5.1
che è un'applicazione Java che implementa un client SOAP. Dandogli l'URI http://localhost/webservice.php?wsdl
dovrebbe portare alla funzione increment
estratta ma non lo farà. Al contrario, viene visualizzato un messaggio di errore: The Value '\Library\IncrementInt' is an invalid name
. Mi sembra che stia riscontrando un problema nell'accettare \
come parte di un nome di tipo. D'altra parte, PHP non può fare a meno di loro.
Per assicurarsi che tutto il resto sia OK, ho testato gli stessi file esatti senza lo spazio dei nomi e funziona senza intoppi.
Qualcuno ha affrontato un problema simile e, cosa più importante, qualcuno sa come superare questo problema?
[UPDATE]
sono riuscito a testare the same scenario with ZF2 e funziona. Forse devo rinunciare a ZF1!
L'implementazione nativa di SOAP in PHP è orribilmente, completamente infranta. Abbiamo segnalato un [non così piccolo bug] (https://bugs.php.net/bug.php?id=50675) nel 2010 presso il mio vecchio posto di lavoro ed è ancora aperto. Anche se non ho controllato, ma le mie scommesse si sarebbero basate su Zend_Soap basato sull'implementazione nativa di SOAP, quindi è meglio fare attenzione. – Janoszen