Php Symfony2使用besimple/SOAP客户端包使用SOAP服务

Php Symfony2使用besimple/SOAP客户端包使用SOAP服务,php,web-services,symfony,soap,wsdl,Php,Web Services,Symfony,Soap,Wsdl,嘿,这一个可能很长 我在Symfony2框架中编写了一个API,我现在正试图用我的API使用一个SOAP服务,这是我以前从未做过的,所以我继续在google上查看Symfony2是否有SOAP捆绑包,并发现了以下内容: 实际的SOAP wsdl: 因此,对于此捆绑包,我有以下设置: 参数.yml soap_options: option1: something option2: somethingElse wsdl: wsdl/Weather.wsdl 在我的src dir中,

嘿,这一个可能很长

我在Symfony2框架中编写了一个API,我现在正试图用我的API使用一个SOAP服务,这是我以前从未做过的,所以我继续在google上查看Symfony2是否有SOAP捆绑包,并发现了以下内容:

实际的SOAP wsdl:

因此,对于此捆绑包,我有以下设置:

参数.yml

soap_options:
  option1:  something
  option2:  somethingElse
  wsdl: wsdl/Weather.wsdl
在我的src dir中,我有一个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);
    }
}

首先,您不必将选项传递到SoapClientWrapper的构造函数中,您可以将它们定义为如下所示的服务:

# app/config/config.yml
be_simple_soap:
  clients:
    WeatherApi:
        # required
        wsdl: http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL
<!-- Soap Client -->
<service id="book.bookbundle.soap.wrapper"
        class="Book\BookBundle\Soap\SoapClientWrapper">
    <argument type="service" id="besimple.soap.client.weatherapi" />
</service>
这将创建一个名为“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);
    }
}

有时您不需要捆绑包来实现某些功能。我遇到了一些SOAP捆绑包的问题,发现了以下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)));
希望这有帮助

<!-- Soap Client -->
<service id="book.bookbundle.soap.wrapper"
        class="Book\BookBundle\Soap\SoapClientWrapper">
    <argument type="service" id="besimple.soap.client.weatherapi" />
</service>
$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)));