Laravel 5.1 consuming soap wsdl service using controller and model

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Consuming SOAP WSDL Services in Laravel: A Modern Approach

Migrating legacy integrations, such as those relying on PHP and Nusoap for SOAP communication, into a modern framework like Laravel is a common requirement. The shift allows us to leverage the Model-View-Controller (MVC) architecture to enforce better separation of concerns, making our code cleaner, more testable, and easier to maintain.

This post will guide you through architecting a solution where you consume an external SOAP WSDL service within a Laravel application, using Eloquent Models to source necessary data for the requests.

The MVC Strategy for External Services

The fundamental principle when integrating external services into a framework is adhering to the MVC pattern. We should avoid placing complex external communication logic directly within the Controller or Model. Instead, we introduce a dedicated Service Layer to handle the specifics of SOAP interaction, keeping our Controllers lean and focused solely on routing and response formatting.

  1. Model (Data Retrieval): The Eloquent Model is responsible for querying your MySQL database to fetch the necessary parameters needed for the SOAP request (e.g., user credentials, specific IDs required by the WSDL).
  2. Service (Business Logic/Communication): A dedicated service class handles the actual communication with the external SOAP endpoint. This is where you instantiate the SOAP client or wrapper and manage complex calls, including authentication logic.
  3. Controller (Request Handling): The Controller receives the HTTP request, validates input, delegates the data fetching to the Model, passes that data to the Service layer, and returns the final response to the user.

This separation is crucial for scalability, aligning with the architectural principles promoted by organizations like Laravel Company. A well-structured application built on these principles is robust and scalable.

Implementing Dynamic SOAP Calls

When making a SOAP call where parameters are dynamic (sourced from your database), the Model feeds the data to the Service, which then constructs the correct request payload for the external service.

Let's assume you need to fetch currency rates based on a user ID stored in your database, and the SOAP service requires a login token.

Step 1: The Eloquent Model (Data Source)

First, ensure you have a model that can retrieve the required data from MySQL.

// app/Models/User.php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $table = 'users';
}

Step 2: The SOAP Service Layer (Communication Logic)

This service handles the interaction with the WSDL. Instead of relying solely on a generic wrapper, we will implement the specific logic for authentication and data injection here.

For this example, let's assume you are using a custom or adapted SOAP client structure to handle user context.

// app/Services/SoapService.php
namespace App\Services;

use App\Models\User;

class SoapService
{
    protected $soapClient;

    public function __construct()
    {
        // Initialize your actual SOAP client setup here
        // For demonstration, we assume a direct call mechanism exists
    }

    /**
     * Fetches data by making an authenticated SOAP call.
     *
     * @param int $userId The ID used for authentication/context.
     * @return array The response from the SOAP service.
     */
    public function getCurrencyData(int $userId): array
    {
        // 1. Retrieve necessary data from the database
        $user = User::findOrFail($userId);
        $username = $user->username;
        $password = $user->password; // In a real app, use secure methods like tokens

        // 2. Configure and execute the SOAP call with dynamic parameters
        // This is where you inject the login details specific to the WSDL requirements.
        $wsdlPath = 'path/to/currency_wsdl';

        // Placeholder for actual SOAP execution logic:
        // $client = new SoapClient($wsdlPath, ['login' => $username, 'password' => $password]);
        // $result = $client->getCurrencyRate(['user_id' => $userId]);
        
        // Mocking the result for demonstration:
        return [
            'success' => true,
            'rate' => 1.25,
            'source_user' => $username
        ];
    }
}

Step 3: The Controller (Request Orchestration)

The Controller orchestrates the flow between the request and the service layer.

// app/Http/Controllers/SoapController.php
namespace App\Http\Controllers;

use App\Services\SoapService;
use Illuminate\Http\Request;

class SoapController extends Controller
{
    protected $soapService;

    public function __construct(SoapService $soapService)
    {
        $this->soapService = $soapService;
    }

    public function requestCurrency(Request $request)
    {
        // 1. Validate input (e.g., ensure the user is authenticated)
        $userId = $request->input('user_id');

        if (!$userId) {
            return response()->json(['error' => 'User ID is required'], 400);
        }

        try {
            // 2. Delegate the complex logic to the Service layer
            $data = $this->soapService->getCurrencyData($userId);

            // 3. Return the structured response
            return response()->json($data, 200);

        } catch (\Exception $e) {
            // Handle SOAP errors or database connection issues gracefully
            return response()->json(['error' => 'Failed to fetch data from external service'], 500);
        }
    }
}

Conclusion

By adopting this structured approach—using Eloquent for data, a dedicated Service layer for communication (handling the complex SOAP WSDL details and authentication), and a lean Controller for orchestration—you successfully transform a brittle PHP integration into a robust, maintainable Laravel feature. This pattern ensures that your application remains clean while effectively interfacing with external systems, proving that modern frameworks like Laravel provide the necessary structure to handle complex business logic efficiently.