Taona's avatar
Level 1

LARAVEL REQUEST SOAP WEBSERVICES

I need to use Laravel to interact with SOAP web services but l can not find a way around it

0 likes
9 replies
alanholmes's avatar

Hi @taona

I've not really worked with SOAP in Laravel, so not sure if it has any inbuilt helpers, but you can always fallback to just using the php SoapClient library directly.

Or there are some packages that are wrappers around this that you could pull in: https://packagist.org/?query=laravel%20soap

Snapey's avatar

Php works perfectly well with soap, especially if the service has a wsdl file available

Taona's avatar
Level 1

thanks a lot @alanholmes, say l need to use SoapClient, can l just use it in Laravel directly to write the php needed. Is it possible or it will distort some designs.

Taona's avatar
Level 1

thanks @snapey , does that mean l can use that native php syntax to work with SOAP right there in a laravel application.

Snapey's avatar

Yes, use the native SOAP functions. You just need to decide where to put them.


    /**
     * Construct a new instance. Calls the soap service and creates an endpoint, gets methods
     * @param App\System $system system model with the various URL and passwords
     */
    public function __construct($system)
    {
        $this->system = $system;

        $this->options = array(
            'soap_version' => SOAP_1_1,
            'exceptions' => true,
            'trace' => 1,
            'cache_wsdl' => WSDL_CACHE_MEMORY,
            'login' => $system['webuser'],
            'password' => $system['webpass'],
            'connection_timeout' => 25,
            'style' => SOAP_RPC,
            'use' => SOAP_ENCODED,
        );

        $this->client = new \SoapClient($this->system['url'] . '?WSDL', $this->options);

    }

The above creates a new soap connection to an endpoint that is passed as an array ($system) - URL and password.

You can then call functions on the $client;

    public function provider($method, $query)
    {

        try {
            $response = $this->client->$method($query);
        } catch (\Exception $e) {

            Log::error('Soap Exception: ' . $e->getMessage());
            throw new \Exception('Problem with SOAP call');
        }
        return $response;
    }

    public function getRecentPlannedConsignments($hours = 1)
    {
        $query = ['hoursToLookBack'=>$hours];

        return $this->provider('GetRecentPlannedConsignments', $query);

    }

In my code I can call functions like getRecentPlannedConsignments() (there are many more)

This calls a function that wraps the soap client call in a try-catch block.

2 likes
Jesus-David-Vidal-Ramirez's avatar

Hola @snapey Estoy probando el servicio pero tengo error, cabe resaltar que no lo hago como lo tienes tu lo hago de esta forma pero recibo un error 500 $response = new SoapClient('url' . '?WSDL') esto lo tengo en un controlador y ejecuto ese endPoint desde postman

Please or to participate in this conversation.