php - 带有 symfony 的 SOAP 网络服务

标签 php web-services symfony soap service

我用 symfony2 编写了一个 Web 服务,位于 tutorial 中.

我的路线是:

_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('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fa979fba9f829b978a969fd4999597" rel="noreferrer noopener nofollow">[email protected]</a>')
            ->setSubject('Hello Service')
            ->setBody($name . ' says hi!');

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

        return 'Hello, '.$name;
    }
}

我的 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>

但我看到以下错误:

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

如何解决这个问题? 感谢您的帮助

最佳答案

我认为您已经从该链接复制了 wsdl 文件的内容,但错过了更改 location部分,您应该更改 <soap:address location="http://example.com/app.php/soap" />到您自己的 URL,例如 http://xxx.xxxx.xxx/unproject/web/app_dev.php/soap ..

关于php - 带有 symfony 的 SOAP 网络服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27106307/

相关文章:

php - X-Sendfile具体配置.htaccess问题

c# - Web 服务客户端和压缩

mysql - 学说使加入

php - 使用 CSS 的下拉菜单

php - Memcache 不会刷新或清除内存

html - 我将如何收集尽可能多的文本 : "[subject] are ..." from the web? 实例

c# - 我们可以将数据集传递给 Web 服务方法吗?如果是,那么如何?

javascript - symfony框架中的Ajax调用

php - 添加集合/实体会使表单渲染非常慢

php - Laravel 调度器 : how to run task on weekdays only?