与symfony的肥皂网服务

时间:2014-11-24 13:44:26

标签: php web-services symfony soap service

我使用#{3}}中的symfony2编写了一个Web服务。

我的溃败是:

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

我的配置:

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

我的服务:

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('me@example.com')
            ->setSubject('Hello Service')
            ->setBody($name . ' says hi!');

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

        return 'Hello, '.$name;
    }
}

我的控制员:

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>

但我看到下面的错误:

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

如何解决此问题? 谢谢你的帮助

2 个答案:

答案 0 :(得分:1)

我认为您已从该链接复制了wsdl文件的内容,但未能更改location部分,您应将<soap:address location="http://example.com/app.php/soap" />更改为您自己的网址,例如{ {1}} ..

答案 1 :(得分:1)

WSDL 中的此行编辑为您在其中公开的URL SoapServer soap:address location="http://example.com/app.php/soap"

同时替换:
targetNamespace="urn:helloservicewsdl
对于那个 的 targetNamespace="urn:arnleadservicewsdl"

另见 php.ini 中的那个 soap.wsdl_cache_enabled=0 &amp;&amp;的 soap.wsdl_cache_ttl=0

进行此项更改对我有用。