APIs are the backbone of modern applications. While RESTful APIs dominate today, many enterprise systems still expose services through the SOAP protocol. As a Laravel developer, you may come across projects where you need to integrate with SOAP web services.
For a complete working project, check out this GitHub repository: MantraIdeas/soapImplementation
In this guide, we’ll cover:
- What is a SOAP API?
- SOAP API vs REST API
- Understanding WSDL and XML-based APIs
- Installing and configuring a SOAP client in Laravel
- Example: Calling a SOAP API in Laravel
- Testing and handling SOAP requests & responses
What is a SOAP API?
SOAP (Simple Object Access Protocol) is a protocol for exchanging structured data between applications over a network. Unlike REST, which typically uses JSON, SOAP APIs rely on XML-based APIs with strict formatting rules.
A SOAP API is usually defined by a WSDL (Web Services Description Language) file, which describes available operations, input/output data types, and the service endpoint.
- Available operations (e.g.,
Add, Subtract) - Input/output data types (e.g.,
int, string) - Service endpoint (e.g.,
http://www.dneonline.com/calculator.asmx)
In short: A WSDL is the contract between client and server.
SOAP API vs REST API
- SOAP API → XML-based, strongly typed, enterprise-focused, uses WSDL for contracts.
- REST API → Lightweight, JSON-based, stateless, more flexible for web and mobile apps.
- GraphQL → Query-based, client-driven, allows fetching only required fields.
In Laravel API development, you may encounter SOAP APIs when integrating with legacy systems, banks, payment gateways, or enterprise APIs.
Step 1: Enable or Install SOAP Client in Laravel
Laravel uses PHP’s built-in SOAP extension. First, make sure it’s enabled.
On Linux / Mac
Edit your php.ini:
extension=soap
Then restart your server:
sudo service php8.2-fpm restart
On Windows (XAMPP / WAMP)
Uncomment the line in php.ini:
;extension=soap
Remove the ; and restart Apache
Verify Installation
Run:
php -m | grep soap
If you see soap in the list, you’re good to go.
Step 2: Create a Service Class for SOAP Integration
Instead of directly calling SOAP in controllers, we’ll create a service class.
<?php
namespace App\Services;
use SoapClient;
use SoapFault;
class CalculatorSoapService
{
private SoapClient $client;
public function __construct()
{
try {
$this->client = new SoapClient('http://www.dneonline.com/calculator.asmx?WSDL');
} catch (SoapFault $e) {
throw new \Exception("SOAP Client Error: " . $e->getMessage());
}
}
public function add(int $a, int $b): int
{
$response = $this->client->__soapCall("Add", [["intA" => $a, "intB" => $b]]);
return $response->AddResult;
}
public function subtract(int $a, int $b): int
{
$response = $this->client->__soapCall("Subtract", [["intA" => $a, "intB" => $b]]);
return $response->SubtractResult;
}
}
Step 3: Use the SOAP Service in a Controller
<?php
namespace App\Http\Controllers;
use App\Services\CalculatorSoapService;
class SoapApiController extends Controller
{
public function calculate()
{
$service = new CalculatorSoapService();
$sum = $service->add(10, 5);
$diff = $service->subtract(10, 5);
return response()->json([
"sum" => $sum,
"difference" => $diff,
]);
}
}
Step 4: Define Routes
In routes/web.php:
use App\Http\Controllers\SoapApiController;
Route::get('/soap-calc', [SoapApiController::class, 'calculate']);
Visit:
http://your-app.test/soap-calcYou should see:
{
"sum": 15,
"difference": 5
}
Step 5: SOAP Request & Response (Behind the Scenes)
When you call the Add method, Laravel generates a SOAP request XML like this:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Add xmlns="http://tempuri.org/">
<intA>10</intA>
<intB>5</intB>
</Add>
</soap:Body>
</soap:Envelope>
And the SOAP API sends back a response:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<AddResponse xmlns="http://tempuri.org/">
<AddResult>15</AddResult>
</AddResponse>
</soap:Body>
</soap:Envelope>
Your Laravel SoapClient automatically converts this into a PHP object.
Step 6: SOAP API Testing
For SOAP API testing, you can use tools like:
- Postman (supports SOAP with raw XML requests).
- SoapUI (dedicated SOAP testing tool).
- PHP Artisan Tinker to manually test SoapClient calls.
Example in Tinker:
php artisan tinker
>>> $service = new App\Services\CalculatorSoapService();
>>> $service->add(20, 30);
=> 50Bonus: Laravel SOAP API Example with REST Integration
Sometimes, you’ll need to wrap a SOAP API in a Laravel REST API so that modern apps can consume it.
Example REST endpoint:
Route::get('/api/add/{a}/{b}', function ($a, $b) {
$service = new \App\Services\CalculatorSoapService();
return response()->json([
'result' => $service->add($a, $b)
]);
});Now your SOAP API is exposed as a RESTful API in Laravel
Final Thoughts
- SOAP API is still widely used in enterprise APIs, banks, and government systems.
- Laravel makes SOAP integration easy with PHP’s built-in SoapClient.
- Always abstract SOAP calls into a service class for cleaner code.
- You can expose SOAP APIs as REST APIs for modern applications.