hamid
hamid

Reputation: 21

soap web service with symfony

I write a web service with symfony2 that is in this tutorial.

my rout is :

_soap:
path:      /soap
defaults:  { _controller: AcmeSoapBundle:Default:index}

my config :

services:
hello_service:
    class: Acme\SoapBundle\Services\HelloService
    arguments: ["@mailer"]

my service :

namespace Acme\SoapBundle\Services;

class HelloService
{
    private $mailer;

    public function __construct(\Swift_Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function hello($name)
    {

        $message = \Swift_Message::newInstance()
            ->setTo('[email protected]')
            ->setSubject('Hello Service')
            ->setBody($name . ' says hi!');

        $this->mailer->send($message);

        return 'Hello, '.$name;
    }
}

my controller :

namespace Acme\SoapBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

class DefaultController extends Controller
{
    public function indexAction()
    {
        $server = new \SoapServer('hello.wsdl');
        $server->setObject($this->get('hello_service'));

        $response = new Response();
        $response->headers->set('Content-Type', 'text/xml; charset=ISO-8859-1');

        ob_start();
        $server->handle();
        $response->setContent(ob_get_clean());

        return $response;
    }
}

hello.wsdl ( web/hello.wsdl )

    <?xml version="1.0" encoding="ISO-8859-1"?>
<definitions xmlns: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:schema targetNamespace="urn:hellowsdl">
            <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
            <xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
        </xsd:schema>
    </types>

    <message name="helloRequest">
        <part name="name" type="xsd:string" />
    </message>

    <message name="helloResponse">
        <part name="return" type="xsd:string" />
    </message>

    <portType name="hellowsdlPortType">
        <operation name="hello">
            <documentation>Hello World</documentation>
            <input message="tns:helloRequest"/>
            <output message="tns:helloResponse"/>
        </operation>
    </portType>

    <binding name="hellowsdlBinding" type="tns:hellowsdlPortType">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="hello">
            <soap:operation soapAction="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>

    <service name="hellowsdl">
        <port name="hellowsdlPort" binding="tns:hellowsdlBinding">
            <soap:address location="http://example.com/app.php/soap" />
        </port>
    </service>
</definitions>

but I see bellow error:

XML Parsing Error: no element found
Location: http://xxx.xxxx.xxx/unproject/web/app_dev.php/soap
Line Number 1, Column 1:

How can I fix this problem ? Thanks for your help

Upvotes: 2

Views: 3997

Answers (2)

Varun
Varun

Reputation: 159

Edit this line in Your WSDL to that of the URL In which you are exposing SoapServer : soap:address location="http://example.com/app.php/soap"

Also Replace :
targetNamespace="urn:helloservicewsdl"
to That of
targetNamespace="urn:arnleadservicewsdl"

Also See That in your php.ini that
soap.wsdl_cache_enabled=0 && soap.wsdl_cache_ttl=0

Making this changes Worked for me.

Upvotes: 1

xurshid29
xurshid29

Reputation: 4210

I think you've copied the contents of wsdl file from that link, but missed to change the location section, you should change <soap:address location="http://example.com/app.php/soap" /> to your own URL, for example, to http://xxx.xxxx.xxx/unproject/web/app_dev.php/soap..

Upvotes: 1

Related Questions