How to Create a SOAP Web Service in a Symfony Controller

How to Create a SOAP Web Service in a Symfony Controller

Setting up a controller to act as a SOAP server is aided by a couple tools. Those tools expect you to have the PHP SOAP extension installed. As the PHP SOAP extension cannot currently generate a WSDL, you must either create one from scratch or use a 3rd party generator.

Note

There are several SOAP server implementations available for use with PHP. Laminas SOAP and NuSOAP are two examples. Although the PHP SOAP extension is used in these examples, the general idea should still be applicable to other implementations.

SOAP works by exposing the methods of a PHP object to an external entity (i.e. the person using the SOAP service). To start, create a class - HelloService - which represents the functionality that you’ll expose in your SOAP service. In this case, the SOAP service will allow the client to call a method called hello, which happens to send an email:

  1. // src/Service/HelloService.php
  2. namespace App\Service;
  3. class HelloService
  4. {
  5. private $mailer;
  6. public function __construct(\Swift_Mailer $mailer)
  7. {
  8. $this->mailer = $mailer;
  9. }
  10. public function hello($name)
  11. {
  12. $message = (new \Swift_Message('Hello Service'))
  13. ->setTo('[email protected]')
  14. ->setBody($name.' says hi!');
  15. $this->mailer->send($message);
  16. return 'Hello, '.$name;
  17. }
  18. }

Next, make sure that your new class is registered as a service. If you’re using the default services configuration, you don’t need to do anything!

Finally, below is an example of a controller that is capable of handling a SOAP request. Because index() is accessible via /soap, the WSDL document can be retrieved via /soap?wsdl:

  1. // src/Controller/HelloServiceController.php
  2. namespace App\Controller;
  3. use App\Service\HelloService;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. class HelloServiceController extends AbstractController
  8. {
  9. /**
  10. * @Route("/soap")
  11. */
  12. public function index(HelloService $helloService)
  13. {
  14. $soapServer = new \SoapServer('/path/to/hello.wsdl');
  15. $soapServer->setObject($helloService);
  16. $response = new Response();
  17. $response->headers->set('Content-Type', 'text/xml; charset=ISO-8859-1');
  18. ob_start();
  19. $soapServer->handle();
  20. $response->setContent(ob_get_clean());
  21. return $response;
  22. }
  23. }

Take note of the calls to ob_start() and ob_get_clean(). These methods control output buffering which allows you to “trap” the echoed output of $server->handle(). This is necessary because Symfony expects your controller to return a Response object with the output as its “content”. You must also remember to set the "Content-Type" header to "text/xml", as this is what the client will expect. So, you use ob_start() to start buffering the STDOUT and use ob_get_clean() to dump the echoed output into the content of the Response and clear the output buffer. Finally, you’re ready to return the Response.

Below is an example calling the service using a native SoapClient client. This example assumes that the index() method in the controller above is accessible via the route /soap:

  1. $soapClient = new \SoapClient('http://example.com/index.php/soap?wsdl');
  2. $result = $soapClient->__soapCall('hello', ['name' => 'Scott']);

An example WSDL is below.

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  3. xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
  6. xmlns:tns="urn:helloservicewsdl"
  7. xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
  8. xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
  9. xmlns="http://schemas.xmlsoap.org/wsdl/"
  10. targetNamespace="urn:helloservicewsdl">
  11. <types>
  12. <xsd:schema targetNamespace="urn:hellowsdl">
  13. <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/"/>
  14. <xsd:import namespace="http://schemas.xmlsoap.org/wsdl/"/>
  15. </xsd:schema>
  16. </types>
  17. <message name="helloRequest">
  18. <part name="name" type="xsd:string"/>
  19. </message>
  20. <message name="helloResponse">
  21. <part name="return" type="xsd:string"/>
  22. </message>
  23. <portType name="hellowsdlPortType">
  24. <operation name="hello">
  25. <documentation>Hello World</documentation>
  26. <input message="tns:helloRequest"/>
  27. <output message="tns:helloResponse"/>
  28. </operation>
  29. </portType>
  30. <binding name="hellowsdlBinding" type="tns:hellowsdlPortType">
  31. <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
  32. <operation name="hello">
  33. <soap:operation soapAction="urn:arnleadservicewsdl#hello" style="rpc"/>
  34. <input>
  35. <soap:body use="encoded" namespace="urn:hellowsdl"
  36. encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  37. </input>
  38. <output>
  39. <soap:body use="encoded" namespace="urn:hellowsdl"
  40. encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
  41. </output>
  42. </operation>
  43. </binding>
  44. <service name="hellowsdl">
  45. <port name="hellowsdlPort" binding="tns:hellowsdlBinding">
  46. <soap:address location="http://example.com/index.php/soap"/>
  47. </port>
  48. </service>
  49. </definitions>

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.