@@ -9,19 +9,79 @@ tools. You must, of course, have the `PHP SOAP`_ extension installed.
9
9
As the PHP SOAP extension can not currently generate a WSDL, you must either
10
10
create one from scratch or use a 3rd party generator.
11
11
12
+ .. note ::
13
+
14
+ There are several SOAP server implementations available for use with
15
+ PHP. `Zend SOAP `_ and `NuSOAP `_ are two examples. Although we use
16
+ the PHP SOAP extension in our examples, the general idea should still
17
+ be applicable to other implementations.
18
+
19
+ First, let's create a class to service our requests.
20
+
21
+ .. code-block :: php
22
+
23
+ class HelloService
24
+ {
25
+
26
+ private $mailer;
27
+
28
+ public function __construct(Swift_Mailer $mailer)
29
+ {
30
+ $this->mailer = $mailer;
31
+ }
32
+
33
+ public function hello($name)
34
+ {
35
+
36
+ $message = Swift_Message::newInstance()
37
+ ->setTo('me@example.com')
38
+ ->setSubject('Hello Service')
39
+ ->setBody($name . ' says hi!');
40
+
41
+ $this->mailer->send($message);
42
+
43
+
44
+ return 'Hello, ' . $name;
45
+ }
46
+
47
+ }
48
+
49
+ Next, we train Symfony to be able to create an instance of our service. Since
50
+ our service sends an e-mail, our service will need a Swift_Mailer, and using
51
+ the Service Container, we can let Symfony set this up for us.
52
+
53
+ .. configuration-block ::
54
+
55
+ .. code-block :: yaml
56
+
57
+ # app/config/config.yml
58
+ services :
59
+ hello_service :
60
+ class : Acme\DemoBundle\Services\HelloService
61
+ arguments : [mailer]
62
+
63
+ .. code-block :: xml
64
+
65
+ <!-- app/config/config.xml -->
66
+ <services >
67
+ <service id =" hello_service" class =" Acme\DemoBundle\Services\HelloService" >
68
+ <argument >mailer</argument >
69
+ </service >
70
+ </services >
71
+
12
72
Below is an example of a controller that is capable of handling a SOAP
13
73
request. If ``indexAction() `` is accessible via the route ``/soap ``, then the
14
74
WSDL document can be retrieved via ``/soap?wsdl ``.
15
75
16
76
.. code-block :: php
17
77
18
- class MySoapController extends Controller
78
+ class HelloServiceController extends Controller
19
79
{
20
80
public function indexAction()
21
81
{
22
82
$server = new \SoapServer('/path/to/hello.wsdl');
23
83
24
- $server->setObject($this);
84
+ $server->setObject($this->get('hello_service') );
25
85
26
86
$response = new Response();
27
87
@@ -36,12 +96,9 @@ WSDL document can be retrieved via ``/soap?wsdl``.
36
96
return $response;
37
97
}
38
98
39
- public function hello($name)
40
- {
41
- return 'Hello, ' . $name . '!';
42
- }
43
99
}
44
100
101
+
45
102
Take note of the calls to ``ob_start() `` and ``ob_get_clean() ``. These
46
103
methods control `output buffering `_ which allows you to "trap" the echoed
47
104
output of ``$server->handle() ``. This is necessary because Symfony expects
@@ -73,7 +130,7 @@ An example WSDL is below.
73
130
xmlns : soap =" http://schemas.xmlsoap.org/wsdl/soap/"
74
131
xmlns : wsdl =" http://schemas.xmlsoap.org/wsdl/"
75
132
xmlns =" http://schemas.xmlsoap.org/wsdl/"
76
- targetNamespace =" urn:arnleadservicewsdl " >
133
+ targetNamespace =" urn:helloservicewsdl " >
77
134
<types >
78
135
<xsd : schema targetNamespace =" urn:hellowsdl" >
79
136
<xsd : import namespace =" http://schemas.xmlsoap.org/soap/encoding/" />
@@ -118,3 +175,5 @@ An example WSDL is below.
118
175
.. _`PHP SOAP` : http://php.net/manual/en/book.soap.php
119
176
.. _`NuSOAP` : http://sourceforge.net/projects/nusoap
120
177
.. _`output buffering` : http://php.net/manual/en/book.outcontrol.php
178
+ .. _`Zend SOAP` : http://framework.zend.com/manual/en/zend.soap.server.html
179
+ .. _`NuSOAP` : http://sourceforge.net/projects/nusoap/
0 commit comments