@@ -66,24 +66,30 @@ can be retrieved via ``/soap?wsdl``::
6666
6767 use App\Service\HelloService;
6868 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
69+ use Symfony\Component\HttpFoundation\Request;
6970 use Symfony\Component\HttpFoundation\Response;
7071 use Symfony\Component\Routing\Annotation\Route;
7172
7273 class HelloServiceController extends AbstractController
7374 {
7475 #[Route('/soap')]
75- public function index(HelloService $helloService)
76+ public function index(HelloService $helloService, Request $request )
7677 {
7778 $soapServer = new \SoapServer('/path/to/hello.wsdl');
7879 $soapServer->setObject($helloService);
7980
8081 $response = new Response();
81- $response->headers->set('Content-Type', 'text/xml; charset=ISO-8859-1');
8282
8383 ob_start();
84- $soapServer->handle();
84+ $soapServer->handle($request->getContent() );
8585 $response->setContent(ob_get_clean());
8686
87+ foreach (headers_list() as $header) {
88+ $header = explode(':', $header, 2);
89+ $response->headers->set($header[0], $header[1]);
90+ }
91+ header_remove();
92+
8793 return $response;
8894 }
8995 }
@@ -92,11 +98,13 @@ Take note of the calls to ``ob_start()`` and ``ob_get_clean()``. These
9298methods control `output buffering `_ which allows you to "trap" the echoed
9399output of ``$server->handle() ``. This is necessary because Symfony expects
94100your 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 ``.
100108
101109Below is an example of calling the service using a native `SoapClient `_ client. This example
102110assumes that the ``index() `` method in the controller above is accessible via
0 commit comments