Skip to content

Commit a958a43

Browse files
committed
Merge branch 'master' of git://github.com/RogerWebb/symfony-docs into RogerWebb-master
2 parents 07d55ef + f99e1f1 commit a958a43

File tree

1 file changed

+66
-7
lines changed

1 file changed

+66
-7
lines changed

cookbook/web_services/php_soap_extension.rst

+66-7
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,79 @@ tools. You must, of course, have the `PHP SOAP`_ extension installed.
99
As the PHP SOAP extension can not currently generate a WSDL, you must either
1010
create one from scratch or use a 3rd party generator.
1111

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+
1272
Below is an example of a controller that is capable of handling a SOAP
1373
request. If ``indexAction()`` is accessible via the route ``/soap``, then the
1474
WSDL document can be retrieved via ``/soap?wsdl``.
1575

1676
.. code-block:: php
1777
18-
class MySoapController extends Controller
78+
class HelloServiceController extends Controller
1979
{
2080
public function indexAction()
2181
{
2282
$server = new \SoapServer('/path/to/hello.wsdl');
2383
24-
$server->setObject($this);
84+
$server->setObject($this->get('hello_service'));
2585
2686
$response = new Response();
2787
@@ -36,12 +96,9 @@ WSDL document can be retrieved via ``/soap?wsdl``.
3696
return $response;
3797
}
3898
39-
public function hello($name)
40-
{
41-
return 'Hello, ' . $name . '!';
42-
}
4399
}
44100
101+
45102
Take note of the calls to ``ob_start()`` and ``ob_get_clean()``. These
46103
methods control `output buffering`_ which allows you to "trap" the echoed
47104
output of ``$server->handle()``. This is necessary because Symfony expects
@@ -73,7 +130,7 @@ An example WSDL is below.
73130
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
74131
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
75132
xmlns="http://schemas.xmlsoap.org/wsdl/"
76-
targetNamespace="urn:arnleadservicewsdl">
133+
targetNamespace="urn:helloservicewsdl">
77134
<types>
78135
<xsd:schema targetNamespace="urn:hellowsdl">
79136
<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
@@ -118,3 +175,5 @@ An example WSDL is below.
118175
.. _`PHP SOAP`: http://php.net/manual/en/book.soap.php
119176
.. _`NuSOAP`: http://sourceforge.net/projects/nusoap
120177
.. _`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

Comments
 (0)