simplesamlphp/xml-soap
SimpleSAMLphp XML-SOAP utilities for handling SOAP-based XML messaging. Provides helper classes and components used in SAML-related integrations where SOAP bindings are required, intended for use within the SimpleSAMLphp ecosystem.
Installation Add the package via Composer in your Laravel project:
composer require simplesamlphp/xml-soap
Ensure your composer.json includes "minimum-stability": "dev" if required.
First Use Case: SOAP Client Initialize a SOAP client in a Laravel service or controller:
use SimpleSAML\XML\SOAP\Client;
$client = new Client('https://example.com/soap-service?wsdl');
$response = $client->call('TargetFunction', ['param1' => 'value1']);
Where to Look First
Http facade or DI container to manage SOAP clients as singletons if needed.SOAP Requests in Controllers Encapsulate SOAP logic in a service class to avoid cluttering controllers:
namespace App\Services;
use SimpleSAML\XML\SOAP\Client;
class SoapService {
protected $client;
public function __construct() {
$this->client = new Client(config('soap.endpoint'));
}
public function fetchData($params) {
return $this->client->call('fetchData', $params);
}
}
Inject the service into controllers:
public function showData(SoapService $soapService) {
$data = $soapService->fetchData(['id' => 123]);
return view('data', compact('data'));
}
Handling Responses
Parse SOAP responses with Laravel’s collect() or custom helpers:
$response = $soapService->fetchData(['id' => 123]);
$data = collect($response)->first()->toArray();
Error Handling Wrap SOAP calls in try-catch blocks:
try {
$response = $client->call('FaultyFunction', []);
} catch (\SimpleSAML\XML\SOAP\Exception\SOAPException $e) {
Log::error('SOAP Error: ' . $e->getMessage());
return response()->json(['error' => 'Service unavailable'], 500);
}
config/soap.php:
return [
'endpoint' => env('SOAP_ENDPOINT', 'https://example.com/soap'),
'timeout' => 30,
];
Log::debug('SOAP Request:', ['params' => $params, 'endpoint' => $client->getEndpoint()]);
$mockHandler = $this->getMockBuilder(\SimpleSAML\XML\SOAP\Client::class)
->disableOriginalConstructor()
->onlyMethods(['call'])
->getMock();
$mockHandler->method('call')->willReturn(['mocked' => 'response']);
Namespace Conflicts
The package uses SimpleSAML namespaces, which may conflict with Laravel’s Simple helpers. Alias the class:
use SimpleSAML\XML\SOAP\Client as SOAPClient;
WSDL Caching The package caches WSDL definitions aggressively. Clear cache if endpoints change:
$client = new SOAPClient('endpoint', ['cache_wsdl' => WSDL_CACHE_NONE]);
Strict Types
SOAP responses may return SimpleXMLElement objects. Cast to arrays or use json_decode():
$arrayResponse = json_decode(json_encode($response), true);
Timeouts Default timeouts may be too short for slow SOAP services. Configure in the client:
$client = new Client('endpoint', [
'connection_timeout' => 60,
'execution_timeout' => 120,
]);
Enable SOAP Traces Enable PHP’s SOAP extension traces for debugging:
ini_set('soap.wsdl_cache_enabled', '0');
ini_set('soap.wsdl_cache_ttl', '0');
Check php://stderr or Laravel logs for raw SOAP requests/responses.
Validate XML
Use DOMDocument to validate SOAP responses:
$dom = new DOMDocument();
if (!$dom->loadXML($response)) {
throw new \Exception('Invalid SOAP response XML');
}
Custom Headers Add SOAP headers via the client’s constructor options:
$client = new Client('endpoint', [
'soapheaders' => new \SoapHeader('namespace', 'headerName', 'headerValue'),
]);
Middleware for SOAP Create Laravel middleware to preprocess SOAP requests:
namespace App\Http\Middleware;
use Closure;
class SoapAuthMiddleware {
public function handle($request, Closure $next) {
$soapClient = new \SimpleSAML\XML\SOAP\Client(config('soap.endpoint'));
$soapClient->setAuthToken($request->bearerToken());
return $next($request);
}
}
Event Dispatching Dispatch Laravel events after SOAP calls:
event(new SoapResponseReceived($response));
How can I help you explore Laravel packages today?