How to create a SOAP Web service inside a symfony controller

Source: Internet
Author: User
Tags soap wsdl example wsdl

It would be very easy to set up a controller as a SOAP service with some tools. First, you must have a PHP SOAP extension installed. Since PHP SOAP extensions cannot generate WSDL now, you can either create your own from scratch and use a third-party builder.

There are some SOAP service interfaces that you can use in PHP. Zend SOAP and Nusoap are two of them. Although PHP SOAP extensions are used in these examples, common ideas can be used in other application interfaces.

Soap works by exposing a PHP object to an external entity (the person using the SOAP service). First, create a HelloService class-representing the functionality you will expose in your soap service. In the following example, the SOAP service allows the client to invoke a Hello method to send a message:

//src/acme/soapbundle/services/helloservice.phpnamespace Acme\soapbundle\services;classhelloservice{Private $mailer;  Public function__construct (\swift_mailer$mailer)    {        $this->mailer =$mailer; }     Public functionHello$name)    {        $message= \swift_message::newinstance ()->setto (' [email protected] ')                                ->setsubject (' Hello Service ')                                ->setbody ($name. ' Says hi! '); $this->mailer->send ($message); return' Hello, '.$name; }}

Next, you can configure Symfony to create an instance of the class. Since the class is used to send mail, he needs to receive a Swift_mailer instance parameter. With the service container, you can configure a Symfony HelloService object well:

# app/config/services.ymlServices:    hello_service:        class: acme\soapbundle\ Services\helloservice        arguments: [' @mailer ']

The following is an example of invoking a SOAP request. If Indexaction () can be successfully accessed through a route, then the WSDL document can be returned.

namespace Acme\soapbundle\controller; UseSymfony\bundle\frameworkbundle\controller\controller; UseSymfony\component\httpfoundation\response;classHelloservicecontrollerextendscontroller{ Public functionindexaction () {$server=New\soapserver ('/path/to/hello.wsdl '); $server->setobject ($this->get (' Hello_service ')); $response=NewResponse (); $response->headers->set (' Content-type ', ' text/xml; Charset=iso-8859-1 '); Ob_start(); $server-handle (); $response->setcontent (Ob_get_clean()); return $response; }}

Write ob_start() down andob_get_clean()这两个方法。这些方法可以控制$server->handle()输出。这是很有必要的,因为symfony希望你的控制器返回的对象是它期望的内容。Content-Type也必须设置为"text/xml",因为这是请求端希望的返回类型。因此,你使用ob_start()缓存输出内容,使用ob_get_clean()打印出要返回的内容并清空缓存,最后返回response。

The following example is a service that invokes the NUSOAP client, assuming that indexaction is reachable successfully through a route.

$client new \soapclient (' http://example.com/app.php/soap?wsdl '); $result $client Array (' name ' = ' Scott ');

The WSDL example is as follows

<?XML version= "1.0" encoding= "Iso-8859-1"?><Definitionsxmlns:soap-env= "http://schemas.xmlsoap.org/soap/envelope/"xmlns:xsd= "Http://www.w3.org/2001/XMLSchema"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:soap-enc= "http://schemas.xmlsoap.org/soap/encoding/"Xmlns:tns= "URN:ARNLEADSERVICEWSDL"Xmlns:soap= "http://schemas.xmlsoap.org/wsdl/soap/"xmlns:wsdl= "http://schemas.xmlsoap.org/wsdl/"xmlns= "http://schemas.xmlsoap.org/wsdl/"targetnamespace= "URN:HELLOSERVICEWSDL">    <Types>        <Xsd:schematargetnamespace= "URN:HELLOWSDL">            <Xsd:importnamespace= "http://schemas.xmlsoap.org/soap/encoding/" />            <Xsd:importnamespace= "http://schemas.xmlsoap.org/wsdl/" />        </Xsd:schema>    </Types>    <messagename= "Hellorequest">        < Partname= "Name"type= "Xsd:string" />    </message>    <messagename= "Helloresponse">        < Partname= "return"type= "Xsd:string" />    </message>    <PortTypename= "Hellowsdlporttype">        <Operationname= "Hello">            <Documentation>Hello World</Documentation>            <inputmessage= "Tns:hellorequest"/>            <Outputmessage= "Tns:helloresponse"/>        </Operation>    </PortType>    <bindingname= "Hellowsdlbinding"type= "Tns:hellowsdlporttype">        <soap:bindingstyle= "RPC"Transport= "Http://schemas.xmlsoap.org/soap/http"/>        <Operationname= "Hello">            <soap:operationSOAPAction= "Urn:arnleadservicewsdl#hello"style= "RPC"/>            <input>                <Soap:body Use= "encoded"namespace= "URN:HELLOWSDL"Encodingstyle= "http://schemas.xmlsoap.org/soap/encoding/"/>            </input>            <Output>                <Soap:body Use= "encoded"namespace= "URN:HELLOWSDL"Encodingstyle= "http://schemas.xmlsoap.org/soap/encoding/"/>            </Output>        </Operation>    </binding>    <Servicename= "HELLOWSDL">        <Portname= "Hellowsdlport"binding= "Tns:hellowsdlbinding">            <soap:address Location= "Http://example.com/app.php/soap" />        </Port>    </Service></Definitions>

How to create a SOAP Web service inside a symfony controller

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.