This Laravel/Lumen tutorial help to consume SOAP request using the PHP SOAP client and laravel, one of the most popular PHP frameworks. I am using PHP soap libs to consume soap requests and send JSON responses using Rest Service.
Web services have become an essential component of modern web applications, enabling seamless integration between different systems and applications.
What’s SOAP WebService
Web services can be accessed using various protocols, including RESTful API, SOAP, and XML-RPC. SOAP (Simple Object Access Protocol) is a widely used protocol for building web services that use XML messages to exchange information between different systems. SOAP offers a standardized way of communication and supports a wide range of data types, making it a popular choice for enterprise-level web services.
PHP soapclient
PHP SoapClient is a built-in PHP class that provides a simple and easy-to-use interface for consuming SOAP-based web services.
Consume Webservice Using Laravel and PHP soapclient
Laravel’s built-in support for consuming web services using SoapClient makes it easy to integrate with SOAP-based APIs.
Sometimes, We have a SOAP calls to access resources at that time we need to create a SOAP request to access data.
We will create a Restful service to access the soap service call using the controller method.
We will use the following files,
routes.php
: This file is used to create a rest route for soap service.Http/Controllers/CurrencyController.php
: This file is responsible to create the controller method of the route.app/Service/CurrencyService.php
: This file is used to create a service method to access SOAP call.
You can also check other recommended tutorials of Lumen/Laravel,
- How to create Queue and Run Jobs using worker in Lumen/Laravel
- Laravel Micro Rest Framework – Simple Example of RESTful API in Lumen
- How to Configure Memcached in Lumen Api Framework
- Simple Example of Laravel 5 Login System Using Sentry
- Authorization and Authentication of Users in laravel 5 Using Sentry
- Simple Laravel Layouts using Blade Template and Bootstrap Theme
We need to enable php_soap
and open_ssl
module into php.ini
file, if You haven’t enabled it yet, please follow the below steps to enable php-soap
libs.
we can verify this by checking the list of loaded PHP extensions using the phpinfo()
function.
<?php phpinfo(); ?>
How to enable php-soap in windows
Please enable PHP soap libs from php.ini
file by uncommenting below the line,extension=php_soap.dll
After that restart Xampp or wamp server.
How to enable php-soap in Linux Ubuntu
We need to install the package by run
below command.
sudo apt-get install php7.0-soap sudo systemctl restart apache2.service
Let’s Start Integrate SOAP with Laravel
Step 1: Included SOAP module into Service CurrencyService.php file.use SoapClient;
Step 2: Created a client method to access Soap WSDL in the service file.
private function _client($F5srv) { $opts = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ) ); $context = stream_context_create($opts); $wsdl = "wsdl path"; try { $this->client = new \SoapClient($wsdl, array( 'stream_context' => $context, 'trace' => true, 'login' => username, 'password' => password) ); return $this->client; } catch ( \Exception $e) { Log::info('Caught Exception in client'. $e->getMessage()); } }
You need to replace the WSDL path with your WSDL URL and change username/password credentials, if WSDL wants authorization using credentials otherwise skip.
Step 3: Created service method to consume soap method with params into CurrencyService.php
file.
public function getCurrency($params) { $this->client = $this->_client($params['config']); try { $result = $this->client->Currency($parms); return $result; } catch (\Exception $e) { Log::info('Caught Exception :'. $e->getMessage()); return $e; // just re-throw it } }
You need to replace 'Currency'
method name with your method name.
Step 4: Created one HTTP Post route entry into routes.php
file.
$app->post('get_currency', 'CurrencyController@getCurrency');
Step 5: Created a method getCurrency
into CurrencyController.php
file.
public function getCurrency(Request $request) { $response = array(); $parameters = $request->json()->all(); $rules = array( 'name' => 'required' ); $messages = array( 'name.required' => 'name is required.' ); $validator = \Validator::make(array('name' => $parameters['name']), $rules, $messages); if(!$validator->fails()) { $response = $this->currencyService->getCurrency($parameters); return response; } else { $errors = $validator->errors(); return response()->json($errors->all()); } }
You need to register the service provider into the app.php
file or use the service as a namespace in the controller file to access the service currencyService class method into the above controller method.
hi sr. I have a doubt. You said that we have to put the file CurrencyService.php in app/Service/CurrencyService.php but the Service folder is no longer used since version 5.1 of laravel. Then where should I put this file if in my version of laravel 5.4 this folder does not exist?? Thanks in advance
This is custom folder name , You can create your one and put them