Implémenter un service Web « Soap » côté serveur avec Typo3
Nous allons voir ici, un exemple très simple pour implémenter un service Web côté serveur Soap avec Typo3.
Pour cela, nous avons besoin d’un fichier wsdl (langage de description des services Web), c’est tout simplement la description de vos services Web soap.
implémentation du plugin Frontend
Tout d’abord, vous devez créer un plugin de base type Frontend.
Voici l’arborescence de notre module (le nom des fichiers et du module est arbitraire):
/ext/pluginname_soap/pi1/class.tx_pluginname_soap_pi1.php
/ext/pluginname_soap/pi1/locallang.xml (votre fichier de langue)
/ext/pluginname_soap/res/wsdl/wsdl.wsdl
Voici le contenu du fichier class.tx_pluginname_soap_pi1.php
<?php ini_set("soap.wsdl_cache_enabled", 0); // Disable wsdl cache ini_set("session.auto_start", 0); // Disable session auto_start session_start(); // start session for soap object persistence // ... require_once(PATH_tslib.'class.tslib_pibase.php'); // ... /** * Plugin '[Soap] WebServices' for the 'pluginname_soap' extension. * This package is just a sample, use it to set SoapServer within Typo3 * * @author Regnier David <regnier_david@yahoo.fr> * @package TYPO3 * @subpackage tx_pluginname_soap */ class tx_pluginname_soap_pi1 extends tslib_pibase { public $prefixId = 'tx_pluginname_soap_pi1'; // Same as class name public $scriptRelPath = 'pi1/class.tx_pluginname_soap_pi1.php'; // Path to this script relative to the extension dir. public $extKey = 'pluginname_soap'; // The extension key. private $wsdlPath; // Wsdl path private $quotes = array("ibm" => 98.42); // Sample value to return /** * The main method of the PlugIn * * @param string $content: The PlugIn content * @param array $conf: The PlugIn configuration * @return The content that is displayed on the website */ public function main($content, $conf) { $this -> wsdlPath = t3lib_extMgm::extPath($this -> extKey).'res/wsdl/wsdl.wsdl'; $this -> callSoapServer(); } /** * Call soapServer * * @return string (soap response) */ private function callSoapServer() { $soapServer = t3lib_div::makeInstance("SoapServer", $this -> wsdlPath); $soapServer -> setClass($this -> prefixId); $soapServer -> setPersistence(SOAP_PERSISTENCE_SESSION); $soapServer -> handle(); } /** * This is just a sample method available for soapServer * * @param string $symbol: Value string * @return string (soap response envelope) */ public function getQuote($symbol) { if (isset($this -> quotes[$symbol])) { return $this -> quotes[$symbol]; } else { throw new SoapFault("Server", "Unknown Symbol '$symbol'."); // Set needed error } } } if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/pluginname_soap/pi1/class.tx_pluginname_soap_pi1.php']) { include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/pluginname_soap/pi1/class.tx_pluginname_soap_pi1.php']); } ?>
Voici le contenu du fichier wsdl
<?xml version ='1.0' encoding ='UTF-8' ?> <definitions name='StockQuote' targetNamespace='http://example.org/StockQuote' xmlns:tns=' http://example.org/StockQuote ' 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/' xmlns='http://schemas.xmlsoap.org/wsdl/'> <message name='getQuoteRequest'> <part name='symbol' type='xsd:string'/> </message> <message name='getQuoteResponse'> <part name='Result' type='xsd:float'/> </message> <portType name='StockQuotePortType'> <operation name='getQuote'> <input message='tns:getQuoteRequest'/> <output message='tns:getQuoteResponse'/> </operation> </portType> <binding name='StockQuoteBinding' type='tns:StockQuotePortType'> <soap:binding style='rpc' transport='http://schemas.xmlsoap.org/soap/http'/> <operation name='getQuote'> <soap:operation soapAction='urn:xmethods-delayed-quotes#getQuote'/> <input> <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> </input> <output> <soap:body use='encoded' namespace='urn:xmethods-delayed-quotes' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> </output> </operation> </binding> <service name='StockQuoteService'> <port name='StockQuotePort' binding='StockQuoteBinding'> <soap:address location='http://typo3_instance/index.php?id=4758'/> </port> </service> </definitions>
Tester notre serveur « Soap » avec un client
<?php $client = new SoapClient("wsdl.wsdl"); print($client -> getQuote("ibm")); ?>
Vous devez implémenter chaque méthode qui va correspondre à une opération dans le fichier wsdl.
N’oubliez pas de faire pointer votre fichier wsdl sur votre instance de Typo3, là où vous avez installé votre plugin, dans notre cas: http://typo3_instance/index.php?id=4758