Symfony2使用besimple / soap-client bundle消费SOAP服务

时间:2016-02-04 15:04:46

标签: php web-services symfony soap wsdl

嘿所有这一个可能很长......

我在Symfony2框架中有一个编写的API,我现在正在尝试使用我以前从未做过的API的SOAP服务,所以我继续谷歌看看Symfony2是否有任何SOAP包,并发现: SOAP Bundle

实际SOAP wsdl:http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL

所以对于这个包我有以下设置:

Parameters.yml

soap_options:
  option1:  something
  option2:  somethingElse
  wsdl: wsdl/Weather.wsdl

在我的src目录中,我有一个Soap目录,它有SoapClientWrapper.php和子目录wsdl:

SoapClientWrapper.php:

<?php
namespace Book\BookBundle\Soap;

use BeSimple\SoapClient\SoapClient;

class SoapClientWrapper extends SoapClient
{
    public function __construct(array $options)
    {
        $wsdl = dirname(__FILE__) . '/' .$options['wsdl'];
        parent::__construct($wsdl, $options);
    }
}

在wsdl目录中,我有一个包含所有xml的Weather.wsdl文件。

我已将SOAP创建为服务,如下所示:

<!-- Soap Client -->
<service id="book.bookbundle.soap.wrapper"
        class="Book\BookBundle\Soap\SoapClientWrapper">
    <argument key="soap_options">%soap_options%</argument>
</service>
然后我将它注入我的一个php文件中,如下所示:

<?php
namespace Book\BookBundle\Dto\Template;

use Book\BookBundle\Soap\SoapClientWrapper;

    /**
     * @var SoapClientWrapper
     */
    private $soap;

    /**
     * @param  SoapClientWrapper $soapClientWrapper
     */
    public function __construct(
        SoapClientWrapper $soapClientWrapper
    ) {
        $this->soap = $soapClientWrapper;
    }

public function soapGreatFunNOT()
{

}

所以我期望在我的功能soapGreatFunNOT中发生的事情是调用我的肥皂服务$ this-&gt; soap传递服务所需的所有参数。但这是我迷路的地方,不能这样做....?我可能在这里遗漏了一些东西或者没有理解......

我测试了这个连接到它的SoapUI的webService传递了成功交互所需的所有参数并得到了响应但很容易,以symfony的方式我迷路了.....?

2 个答案:

答案 0 :(得分:1)

有时您不需要捆绑来实现某些功能。我遇到了一些SOAP包的问题,​​发现了以下PHP类:

http://php.net/manual/es/class.soapclient.php

您可以直接使用它来使用SOAP服务:

$client = new \SoapClient('http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL');

// useful information about the service
dump($client->__getFunctions());
dump($client->__getTypes());

// function call without parameters
dump($client->getWeatherInformation());

// function call with parameters
dump($client->getCityWeatherByZIP(array('ZIP' => 75220)));

希望这有帮助!

答案 1 :(得分:0)

首先,您不必将选项传递给SoapClientWrapper的构造函数,您可以将它们定义为这样的服务:

# app/config/config.yml
be_simple_soap:
  clients:
    WeatherApi:
        # required
        wsdl: http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL

这将创建一个名为“besimple.soap.client.weatherapi”的服务,您可以将其注入您在symfony应用程序中定义的任何其他服务。

假设你想在控制器中使用它。你这样做是这样的:

<?php
namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class WeatherApiController extends Controller
{
    /**
    * @Route("/")
    */
    public function testAction($name)
    {
        $client = $this->container->get('besimple.soap.client.weatherapi');

        $helloResult = $client->GetCityForecastByZIP(array('ZIP' => '66101'));

        return new Response($helloResult);
    }
}

您也可以使用Symfony DI组件将此服务注入其他服务:

<!-- Soap Client -->
<service id="book.bookbundle.soap.wrapper"
        class="Book\BookBundle\Soap\SoapClientWrapper">
    <argument type="service" id="besimple.soap.client.weatherapi" />
</service>