@@ -66,24 +66,30 @@ can be retrieved via ``/soap?wsdl``::
66
66
67
67
use App\Service\HelloService;
68
68
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
69
+ use Symfony\Component\HttpFoundation\Request;
69
70
use Symfony\Component\HttpFoundation\Response;
70
71
use Symfony\Component\Routing\Annotation\Route;
71
72
72
73
class HelloServiceController extends AbstractController
73
74
{
74
75
#[Route('/soap')]
75
- public function index(HelloService $helloService)
76
+ public function index(HelloService $helloService, Request $request )
76
77
{
77
78
$soapServer = new \SoapServer('/path/to/hello.wsdl');
78
79
$soapServer->setObject($helloService);
79
80
80
81
$response = new Response();
81
- $response->headers->set('Content-Type', 'text/xml; charset=ISO-8859-1');
82
82
83
83
ob_start();
84
- $soapServer->handle();
84
+ $soapServer->handle($request->getContent() );
85
85
$response->setContent(ob_get_clean());
86
86
87
+ foreach (headers_list() as $header) {
88
+ $header = explode(':', $header, 2);
89
+ $response->headers->set($header[0], $header[1]);
90
+ }
91
+ header_remove();
92
+
87
93
return $response;
88
94
}
89
95
}
@@ -92,11 +98,13 @@ Take note of the calls to ``ob_start()`` and ``ob_get_clean()``. These
92
98
methods control `output buffering `_ which allows you to "trap" the echoed
93
99
output of ``$server->handle() ``. This is necessary because Symfony expects
94
100
your controller to return a ``Response `` object with the output as its "content".
95
- You must also remember to set the ``"Content-Type" `` header to ``"text/xml" ``, as
96
- this is what the client will expect. So, you use ``ob_start() `` to start
97
- buffering the STDOUT and use ``ob_get_clean() `` to dump the echoed output
98
- into the content of the Response and clear the output buffer. Finally, you're
99
- ready to return the ``Response ``.
101
+ So, you use ``ob_start() `` to start buffering the STDOUT and use
102
+ ``ob_get_clean() `` to dump the echoed output into the content of the Response
103
+ and clear the output buffer. Since ``$server->handle() `` can set headers it is
104
+ also necessary to "trap" these. For this we use ``headers_list `` which provides
105
+ the set headers, these are then parsed and added into the Response after which
106
+ ``header_remove `` is used to remove the headers and to avoid duplicates.
107
+ Finally, you're ready to return the ``Response ``.
100
108
101
109
Below is an example of calling the service using a native `SoapClient `_ client. This example
102
110
assumes that the ``index() `` method in the controller above is accessible via
0 commit comments