4
4
How to Create a SOAP Web Service in a Symfony2 Controller
5
5
=========================================================
6
6
7
- Setting up a controller to act as a SOAP server is simple with a couple
8
- tools. You must, of course, have the `PHP SOAP `_ extension installed.
9
- As the PHP SOAP extension can not currently generate a WSDL, you must either
7
+ Setting up a controller to act as a SOAP server is simple with a couple
8
+ tools. You must, of course, have the `PHP SOAP `_ extension installed.
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
12
.. note ::
13
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
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
17
be applicable to other implementations.
18
18
19
19
SOAP works by exposing the methods of a PHP object to an external entity
@@ -22,8 +22,8 @@ which represents the functionality that you'll expose in your SOAP service.
22
22
In this case, the SOAP service will allow the client to call a method called
23
23
``hello ``, which happens to send an email::
24
24
25
- // src/Acme/SoapBundle/HelloService.php
26
- namespace Acme\SoapBundle;
25
+ // src/Acme/SoapBundle/Services/ HelloService.php
26
+ namespace Acme\SoapBundle\Services ;
27
27
28
28
class HelloService
29
29
{
@@ -58,32 +58,33 @@ a ``HelloService`` object properly:
58
58
59
59
.. code-block :: yaml
60
60
61
- # app/config/config.yml
61
+ # app/config/config.yml
62
62
services :
63
63
hello_service :
64
- class : Acme\DemoBundle \Services\HelloService
64
+ class : Acme\SoapBundle \Services\HelloService
65
65
arguments : [@mailer]
66
66
67
67
.. code-block :: xml
68
68
69
69
<!-- app/config/config.xml -->
70
70
<services >
71
- <service id =" hello_service" class =" Acme\DemoBundle \Services\HelloService" >
71
+ <service id =" hello_service" class =" Acme\SoapBundle \Services\HelloService" >
72
72
<argument type =" service" id =" mailer" />
73
73
</service >
74
74
</services >
75
75
76
- Below is an example of a controller that is capable of handling a SOAP
77
- request. If ``indexAction() `` is accessible via the route ``/soap ``, then the
76
+ Below is an example of a controller that is capable of handling a SOAP
77
+ request. If ``indexAction() `` is accessible via the route ``/soap ``, then the
78
78
WSDL document can be retrieved via ``/soap?wsdl ``.
79
79
80
80
.. code-block :: php
81
81
82
82
namespace Acme\SoapBundle\Controller;
83
83
84
84
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
85
+ use Symfony\Component\HttpFoundation\Response;
85
86
86
- class HelloServiceController extends Controller
87
+ class HelloServiceController extends Controller
87
88
{
88
89
public function indexAction()
89
90
{
@@ -101,17 +102,17 @@ WSDL document can be retrieved via ``/soap?wsdl``.
101
102
}
102
103
}
103
104
104
- Take note of the calls to ``ob_start() `` and ``ob_get_clean() ``. These
105
- methods control `output buffering `_ which allows you to "trap" the echoed
105
+ Take note of the calls to ``ob_start() `` and ``ob_get_clean() ``. These
106
+ methods control `output buffering `_ which allows you to "trap" the echoed
106
107
output of ``$server->handle() ``. This is necessary because Symfony expects
107
108
your controller to return a ``Response `` object with the output as its "content".
108
109
You must also remember to set the "Content-Type" header to "text/xml", as
109
- this is what the client will expect. So, you use ``ob_start() `` to start
110
- buffering the STDOUT and use ``ob_get_clean() `` to dump the echoed output
111
- into the content of the Response and clear the output buffer. Finally, you're
110
+ this is what the client will expect. So, you use ``ob_start() `` to start
111
+ buffering the STDOUT and use ``ob_get_clean() `` to dump the echoed output
112
+ into the content of the Response and clear the output buffer. Finally, you're
112
113
ready to return the ``Response ``.
113
114
114
- Below is an example calling the service using `NuSOAP `_ client. This example
115
+ Below is an example calling the service using `NuSOAP `_ client. This example
115
116
assumes that the ``indexAction `` in the controller above is accessible via the
116
117
route ``/soap ``::
117
118
0 commit comments