John
John

Reputation: 1655

Symfony2 consuming SOAP service using besimple/soap-client bundle

Hey All this one may be long one...

I have a written API in Symfony2 framework, I am now trying to consume a SOAP service with my API which I have never done this before, So I went on to google see if there any SOAP bundles for Symfony2 and found this: SOAP Bundle.

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

So for this bundle I have the following set up:

Parameters.yml

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

In my src dir I have a Soap Directory which has SoapClientWrapper.php and Sub directory 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);
    }
}

In wsdl directory i have a Weather.wsdl file that contains all the xml.

I have created my SOAP as a service like so:

<!-- Soap Client -->
<service id="book.bookbundle.soap.wrapper"
        class="Book\BookBundle\Soap\SoapClientWrapper">
    <argument key="soap_options">%soap_options%</argument>
</service>

I then inject it into one of my php files like so:

<?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()
{

}

So what I would expect to happen in my great function soapGreatFunNOT is to call my soap service $this->soap pass all the required params for the service. But this is where I am getting lost and cannot do this....? I may be missing something or not understanding something here...

I tested this webService with SoapUI connected to it passed all the parameters required for successful interaction and got response but thats easy, In symfony way I am lost.....?

Upvotes: 3

Views: 8246

Answers (2)

Dani Garval
Dani Garval

Reputation: 139

Sometimes you don't need a bundle to achieve some functionality. I was facing problems with some SOAP bundles and found the following PHP class:

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

You can directly use it to consume a SOAP 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)));

Hope this helps!

Upvotes: 2

xocasdashdash
xocasdashdash

Reputation: 538

First you don't have to pass the options into the constructor of your SoapClientWrapper you can define them as a service like this:

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

This would create a service named "besimple.soap.client.weatherapi" that you could inject into any other service you define in your symfony app.

Let's say you want to use it in a controller. You'd do that like so:

<?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);
    }
}

You could also inject this service into other services using the Symfony DI component like this:

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

Upvotes: 0

Related Questions