| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * This example shows how to mark up a server object so that WSDL files may be |
|---|
| 4 | * dynamicaly generated by the SOAP server. This also provides access to a |
|---|
| 5 | * generated DISCO document. The fact that this example has an MP3 class is |
|---|
| 6 | * in no way related to DISCO. ;) |
|---|
| 7 | * |
|---|
| 8 | * DISCO: http://msdn.microsoft.com/msdnmag/issues/02/02/xml/default.aspx |
|---|
| 9 | * |
|---|
| 10 | * Urls accessing this server would look like: |
|---|
| 11 | * - http://localhost/disco_server.php?wsdl (generate WSDL file) |
|---|
| 12 | * - http://localhost/disco_server.php (GET request generates DISCO) |
|---|
| 13 | * - http://localhost/disco_server.php (POST for normal SOAP requests) |
|---|
| 14 | * |
|---|
| 15 | * PHP versions 4 and 5 |
|---|
| 16 | * |
|---|
| 17 | * LICENSE: This source file is subject to version 2.02 of the PHP license, |
|---|
| 18 | * that is bundled with this package in the file LICENSE, and is available at |
|---|
| 19 | * through the world-wide-web at http://www.php.net/license/2_02.txt. If you |
|---|
| 20 | * did not receive a copy of the PHP license and are unable to obtain it |
|---|
| 21 | * through the world-wide-web, please send a note to [email protected] so we can |
|---|
| 22 | * mail you a copy immediately. |
|---|
| 23 | * |
|---|
| 24 | * @category Web Services |
|---|
| 25 | * @package SOAP |
|---|
| 26 | * @author Shane Caraveo <[email protected]> Port to PEAR and more |
|---|
| 27 | * @author Jan Schneider <[email protected]> Maintenance |
|---|
| 28 | * @copyright 2003-2007 The PHP Group |
|---|
| 29 | * @license http://www.php.net/license/2_02.txt PHP License 2.02 |
|---|
| 30 | * @link http://pear.php.net/package/SOAP |
|---|
| 31 | */ |
|---|
| 32 | |
|---|
| 33 | error_reporting(E_ALL); |
|---|
| 34 | |
|---|
| 35 | /** SOAP_Server */ |
|---|
| 36 | require_once 'SOAP/Server.php'; |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | * MP3 database class. |
|---|
| 40 | * |
|---|
| 41 | * @package SOAP |
|---|
| 42 | */ |
|---|
| 43 | class MP3DB_Class |
|---|
| 44 | { |
|---|
| 45 | |
|---|
| 46 | var $__dispatch_map = array(); |
|---|
| 47 | var $__typedef = array(); |
|---|
| 48 | |
|---|
| 49 | function MP3DB_Class() |
|---|
| 50 | { |
|---|
| 51 | /* The only way to describe all methods in WSDL (messages, |
|---|
| 52 | * PortType-operations and bindings) is to use __dispatch_map to |
|---|
| 53 | * describe every method (even methods using simple data types in 'in' |
|---|
| 54 | * and 'out' parameters...) */ |
|---|
| 55 | $this->__dispatch_map['SayHallo'] = |
|---|
| 56 | array('in' => array('input' => 'string'), |
|---|
| 57 | 'out' => array('return' => 'string')); |
|---|
| 58 | $this->__dispatch_map['SayThisNTimes'] = |
|---|
| 59 | array('in' => array('SayThis'=>'string','NTimes' => 'int'), |
|---|
| 60 | 'out' => array('return' => '{urn:MP3DB}ArrayOfStrings')); |
|---|
| 61 | $this->__dispatch_map['GetMP3Tracks'] = |
|---|
| 62 | array('in' => array('query' => 'string'), |
|---|
| 63 | 'out' => array('return' => '{urn:MP3DB}GetMP3TracksResult')); |
|---|
| 64 | $this->__dispatch_map['AddMP3Track'] = |
|---|
| 65 | array('in' => array('MP3Track' => '{urn:MP3DB}MP3Track'), |
|---|
| 66 | 'out' => array('return' => '{urn:MP3DB}AddMP3TrackResult')); |
|---|
| 67 | |
|---|
| 68 | /* Use __typedef to describe userdefined Types in WSDL. Structs and |
|---|
| 69 | * one-dimensional arrays are supported. |
|---|
| 70 | * |
|---|
| 71 | * Struct example: |
|---|
| 72 | * $this->__typedef['TypeName'] = array('VarName' => 'xsdType', ... ); |
|---|
| 73 | * or |
|---|
| 74 | * $this->__typedef['TypeName'] = array('VarName' => '{namespace}SomeOtherType'); |
|---|
| 75 | * |
|---|
| 76 | * Array example: |
|---|
| 77 | * $this->__typedef['TypeName'] = array(array('item' => 'xsdType')); |
|---|
| 78 | * or |
|---|
| 79 | * $this->__typedef['TypeName'] = array(array('item' => '{namespace}SomeOtherType')); |
|---|
| 80 | */ |
|---|
| 81 | |
|---|
| 82 | /* Struct 'MP3Track'. */ |
|---|
| 83 | $this->__typedef['MP3Track'] = |
|---|
| 84 | array('Title' => 'string', |
|---|
| 85 | 'Artist' => 'string', |
|---|
| 86 | 'Album' => 'string', |
|---|
| 87 | 'Year' => 'int', |
|---|
| 88 | 'Genre' => 'int', |
|---|
| 89 | 'Comment' => 'string', |
|---|
| 90 | 'Composer' => 'string', |
|---|
| 91 | 'Orig_Artist' => 'string', |
|---|
| 92 | 'URL' => 'string', |
|---|
| 93 | 'Encoded_by' => 'string'); |
|---|
| 94 | |
|---|
| 95 | /* MP3TracksArray - array of 'MP3Track' structs. */ |
|---|
| 96 | $this->__typedef['MP3TracksArray'] = |
|---|
| 97 | array(array('item' => '{urn:MP3DB}MP3Track')); |
|---|
| 98 | |
|---|
| 99 | /* Struct 'MethodDebug'. */ |
|---|
| 100 | $this->__typedef['MethodDebug'] = |
|---|
| 101 | array('rc' => 'boolean', |
|---|
| 102 | 'ErrNo' => 'int', |
|---|
| 103 | 'Error' => 'string'); |
|---|
| 104 | |
|---|
| 105 | /* Return Struct of method GetMP3Tracks. */ |
|---|
| 106 | $this->__typedef['GetMP3TracksResult'] = |
|---|
| 107 | array('MethodDebug' => '{urn:MP3DB}MethodDebug', |
|---|
| 108 | 'MP3Tracks' => '{urn:MP3DB}MP3TracksArray'); |
|---|
| 109 | |
|---|
| 110 | /* Return Struct of method AddMP3Track. */ |
|---|
| 111 | $this->__typedef['AddMP3TrackResult'] = |
|---|
| 112 | array('MethodDebug' => '{urn:MP3DB}MethodDebug'); |
|---|
| 113 | |
|---|
| 114 | /* Array of strings. */ |
|---|
| 115 | $this->__typedef['ArrayOfStrings'] = |
|---|
| 116 | array(array('item'=>'string')); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | function SayHallo($name) |
|---|
| 120 | { |
|---|
| 121 | return 'Hello, ' . $name; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | function SayThisNTimes($SayThis, $NTimes) |
|---|
| 126 | { |
|---|
| 127 | for ($i = 0; $i < $NTimes; $i++) { |
|---|
| 128 | $return[$i] = $SayThis . ' ' . $i; |
|---|
| 129 | } |
|---|
| 130 | return new SOAP_Value('return', '{urn:MP3DB}ArrayOfStrings', $return); |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | function GetMP3Tracks($query = '') |
|---|
| 134 | { |
|---|
| 135 | for ($i = 0; $i < 5; $i++) { |
|---|
| 136 | $this->MP3Tracks[$i] = new SOAP_Value( |
|---|
| 137 | 'item', |
|---|
| 138 | '{urn:MP3DB}MP3Track', |
|---|
| 139 | array('Title' => new SOAP_Value('Title', 'string', 'some track $i'), |
|---|
| 140 | 'Artist' => new SOAP_Value('Artist', 'string', 'some artist $i'), |
|---|
| 141 | 'Album' => new SOAP_Value('Album', 'string', 'some album $i'), |
|---|
| 142 | 'Year' => new SOAP_Value('Year', 'int', 1999), |
|---|
| 143 | 'Genre' => new SOAP_Value('Genre', 'int', 100), |
|---|
| 144 | 'Comment' => new SOAP_Value('Comment', 'string', 'blabla $i'), |
|---|
| 145 | 'Composer' => new SOAP_Value('Composer', 'string', ''), |
|---|
| 146 | 'Orig_Artist' => new SOAP_Value('Orig_Artist', 'string', ''), |
|---|
| 147 | 'URL' => new SOAP_Value('URL', 'string', ''), |
|---|
| 148 | 'Encoded_by' => new SOAP_Value('Encoded_by', 'string', ''))); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | $MethodDebug['rc'] = new SOAP_Value('rc', 'boolean', true); |
|---|
| 152 | $MethodDebug['ErrNo'] = new SOAP_Value('ErrNo', 'int', 0); |
|---|
| 153 | $MethodDebug['Error'] = new SOAP_Value('Error', 'string', ''); |
|---|
| 154 | |
|---|
| 155 | return new SOAP_Value( |
|---|
| 156 | 'return', |
|---|
| 157 | '{urn:MP3DB}GetMP3TracksResult', |
|---|
| 158 | array('MethodDebug' => new SOAP_Value('MethodDebug', '{urn:MP3DB}MethodDebug',$MethodDebug), |
|---|
| 159 | 'MP3Tracks' => new SOAP_Value('MP3Tracks', '{urn:MP3DB}MP3TracksArray',$this->MP3Tracks))); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | function AddMP3Track($MP3Track) |
|---|
| 163 | { |
|---|
| 164 | /* Well, let's imagine here some code for adding given mp3track to db |
|---|
| 165 | * or whatever... */ |
|---|
| 166 | $MethodDebug['rc'] = new SOAP_Value('rc', 'boolean', true); |
|---|
| 167 | $MethodDebug['ErrNo'] = new SOAP_Value('ErrNo', 'int', 0); |
|---|
| 168 | $MethodDebug['Error'] = new SOAP_Value('Error', 'string', ''); |
|---|
| 169 | |
|---|
| 170 | return new SOAP_Value( |
|---|
| 171 | 'return', |
|---|
| 172 | '{urn:MP3DB}AddMP3TrackResult', |
|---|
| 173 | array('MethodDebug' => new SOAP_Value('MethodDebug', '{urn:MP3DB}MethodDebug', $MethodDebug))); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | function __dispatch($methodname) |
|---|
| 177 | { |
|---|
| 178 | if (isset($this->__dispatch_map[$methodname])) { |
|---|
| 179 | return $this->__dispatch_map[$methodname]; |
|---|
| 180 | } |
|---|
| 181 | return null; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | $server = new SOAP_Server(); |
|---|
| 187 | $server->_auto_translation = true; |
|---|
| 188 | $MP3DB_Class = new MP3DB_Class(); |
|---|
| 189 | $server->addObjectMap($MP3DB_Class, 'urn:MP3DB'); |
|---|
| 190 | |
|---|
| 191 | if (isset($_SERVER['REQUEST_METHOD']) && |
|---|
| 192 | $_SERVER['REQUEST_METHOD'] == 'POST') { |
|---|
| 193 | $server->service($HTTP_RAW_POST_DATA); |
|---|
| 194 | } else { |
|---|
| 195 | require_once 'SOAP/Disco.php'; |
|---|
| 196 | $disco = new SOAP_DISCO_Server($server, 'MP3DB'); |
|---|
| 197 | header('Content-type: text/xml'); |
|---|
| 198 | if (isset($_SERVER['QUERY_STRING']) && |
|---|
| 199 | strpos($_SERVER['QUERY_STRING'], 'wsdl') !== false) { |
|---|
| 200 | echo $disco->getWSDL(); |
|---|
| 201 | } else { |
|---|
| 202 | echo $disco->getDISCO(); |
|---|
| 203 | } |
|---|
| 204 | } |
|---|