Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<com:TContent ID="body" >
2
 
3
<h1 id="134038">SOAP Service</h1>
4
 
5
<p id="670450" class="block-content">
6
SOAP forms the foundation layer of the Web services stack. It provides a neat way for PHP applications to communicate with each other or with applications written in other languages. PRADO provides <tt>TSoapService</tt> that makes developing a SOAP server application an extremely easy task.
7
</p>
8
 
9
<p id="670451" class="block-content">
10
To use <tt>TSoapService</tt>, configure it in the application specification like following:
11
</p>
12
<com:TTextHighlighter Language="xml" CssClass="source block-content" id="code_670133">
13
<services>
14
  <service id="soap" class="System.Web.Services.TSoapService">
15
    <soap id="stockquote" provider="path.to.StockQuote" />
16
    <!--
17
    <soap...other soap service... />
18
    -->
19
  </service>
20
</services>
21
</com:TTextHighlighter>
22
<p id="670452" class="block-content">
23
The example specifies a SOAP service provider named <tt>stockquote</tt> which implements the <tt>getPrice</tt> SOAP method in the provider class <tt>StockQuote</tt>,
24
</p>
25
<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_670134">
26
class StockQuote
27
{
28
	/**
29
	 * @param string $symbol the symbol of the stock
30
	 * @return float the stock price
31
	 * @soapmethod
32
	 */
33
	public function getPrice($symbol)
34
	{
35
		....return stock price for $symbol
36
	}
37
}
38
</com:TTextHighlighter>
39
 
40
<div class="note"><b class="tip">Note:</b>
41
<tt>TSoapService</tt> is based on <a href="http://www.php.net/manual/en/ref.soap.php">PHP SOAP extension</a> and thus requires the extension to be installed.
42
</div>
43
 
44
<p id="670453" class="block-content">
45
With the above simple code, we already finish a simple SOAP service that allows other applications to query the price of a specific stock. For example, a typical SOAP client may be written as follows to query the stock price of IBM,
46
</p>
47
<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_670135">
48
$client=new SoapClient('http://path/to/index.php?soap=stockquote.wsdl');
49
echo $client->getPrice('IBM');
50
</com:TTextHighlighter>
51
 
52
<p id="670454" class="block-content">
53
Notice the URL used to construct <tt>SoapClient</tt> (a class provided by PHP SOAP extension). This is the URL for the <a href="http://en.wikipedia.org/wiki/WSDL">WSDL</a> that describes the communication protocol for the SOAP service we just implemented. WSDL is often too complex to be manually written. Fortunately, <tt>TSoapService</tt> can generate this for us using a WSDL generator. In general, the URL for the automatically generated WSDL in PRADO has the following format:
54
</p>
55
<com:TTextHighlighter Language="xml" CssClass="source block-content" id="code_670136">
56
http://path/to/index.php?SoapServiceID=SoapProviderID.wsdl
57
</com:TTextHighlighter>
58
 
59
<p id="670455" class="block-content">
60
In order for the WSDL generator to generate WSDL for a SOAP service, the provider class needs to follow certain syntax. In particular, for methods to be exposed as SOAP methods, a keyword <tt>@soapmethod</tt> must appear in the phpdoc comment of the method with the following lines specifying method parameters and return value:
61
</p>
62
<ul id="u1" class="block-content">
63
<li>parameter: <tt>@param parameter-type $parameter-name description</tt></li>
64
<li>return value: <tt>@return value-type description</tt></li>
65
</ul>
66
<p id="670456" class="block-content">
67
Valid parameter and return types include: <tt>string</tt>, <tt>int</tt>, <tt>boolean</tt>, <tt>float</tt>, <tt>array</tt>, <tt>mixed</tt>, etc. You may also specify a class name as the type, which translates into a complex SOAP type. For example, for a complex type <tt>Contact</tt>
68
</p>
69
 
70
<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_670137">
71
/**
72
 * Extends TComponent to provide property setter/getter methods
73
 */
74
class Contact {
75
    /**
76
     * @var string $name
77
     * @soapproperty
78
     */
79
    public $name;
80
 
81
    /**
82
     * @var Address $address
83
     * @soapproperty
84
     */
85
    private $_address;
86
 
87
    public function setAddress($value) {
88
        $this->_address=$value;
89
    }
90
 
91
    public function getAddress() {
92
        if($this->_address===null)
93
            $this->_address=new Address;
94
        return $this->_address;
95
    }
96
}
97
 
98
class Address{
99
    /**
100
     * @var string $city
101
     * @soapproperty
102
     */
103
    public $city;
104
}
105
 
106
class ContactManager {
107
    /**
108
     * @return Contact[] an array of contacts
109
     * @soapmethod
110
     */
111
    function getAllContacts() {
112
        return array(new Contact);
113
    }
114
 
115
    /**
116
     * @return Contact one contact
117
     * @soapmethod
118
     */
119
    function getContact($name) {
120
        return new Contact;
121
    }
122
}
123
</com:TTextHighlighter>
124
<p id="670457" class="block-content">For a complex soap object, the properties of the object are specified with
125
<tt>@soapproperty</tt> keyword in the property phpdocs. Furthermore, the
126
propert's type name must be specified as <tt>@var type $name</tt> where <tt>type
127
</tt> is any valid type in mentioned earlier and <tt>$name</tt> will defined
128
a property <tt>name</tt> (notice that if your class is a TComponent, you can
129
provide property setter/getter methods).
130
</p>
131
<p id="670458" class="block-content">
132
An array of complex objects can also be returned by adding a pair of
133
enclosing square brackets after the type name. For example, to return an
134
array of <tt>Contact</tt> type, we define <tt>@return Contact[] ...</tt>.
135
</p>
136
 
137
<div class="tip"><b class="note">Tip:</b>
138
A very useful tool to test out your web services is the free tool
139
<a href="http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=65a1d4ea-0f7a-41bd-8494-e916ebc4159c">WebServiceStudio 2.0</a>. It can invoke webmethods interactively. The user can provide a WSDL endpoint. On clicking button Get the tool fetches the WSDL, generates .NET proxy from the WSDL and displays the list of methods available. The user can choose any method and provide the required input parameters.
140
The tool requires a MS .NET runtime to be installed.
141
 
142
<p id="690008" class="block-content">A similar tool is available for Mac OS X Tiger from
143
<a href="http://www.ditchnet.org/soapclient/">http://www.ditchnet.org/soapclient/</a>
144
</p>
145
</div>
146
 
147
<p id="670459" class="block-content">
148
<tt>TSoapService</tt> may be configured and customized in several ways. In the example above, the <tt>&lt;soap&gt;</tt> element actually specifies a SOAP service using the default <tt>TSoapServer</tt> implementation. Attributes in <tt>&lt;soap&gt;</tt> are passed to <tt>TSoapServer</tt> as its initial property values. For example, the <tt>provider</tt> attribute initializes the <tt>Provider</tt> property of <tt>TSoapServer</tt>. By setting <tt>SessionPersistent</tt> to be true in <tt>&lt;soap&gt;</tt> element, the provider instance will persist within the user session. You may develop your own SOAP server class and use it by specifying the <tt>class</tt> attribute of <tt>&lt;soap&gt;</tt>.
149
</p>
150
 
151
<p id="670460" class="block-content">By default, PHP's soap server will create objects of the type <tt>StdClass</tt>
152
when objects are received from the client. The soap server can be configured to
153
automatically create objects of certain type objects are received as method
154
parameters. For example, if we have a Soap method that accepts
155
a <tt>Contact</tt> object as parameter.
156
<com:TTextHighlighter Language="php" CssClass="source block-content" id="code_670138">
157
/**
158
 * @param Contact $contact
159
 * @return boolean true if saved, false otherwise
160
 * @soapmethod
161
 */
162
 function save(Contact $contact)
163
 {
164
	return true
165
 }
166
</com:TTextHighlighter>
167
The do this, we need to set the <tt>ClassMaps</tt> property of
168
the <tt>TSoapServer</tt> in the <tt>&lt;soap&gt;</tt> tags as
169
a comma separated string of class names that we wish to be automatically
170
converted.
171
<com:TTextHighlighter Language="xml" CssClass="source block-content" id="code_670139">
172
<soap id="contact-manager" provider="path.to.ContactManager"
173
	ClassMaps="Contact, Address"/>
174
</com:TTextHighlighter>
175
</p>
176
 
177
<div class="last-modified">$Id: SoapService.page 1902 2007-05-07 04:17:37Z wei $</div></com:TContent>