1. Go to this page and download the library: Download siberfx/laravel-soap library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Siberfx\Soap\Facades\Soap;
use Siberfx\Soap\Service;
public function boot(): void
{
Soap::add('Weather', function (Service $service) {
$service
->wsdl(config('services.weather.wsdl'))
->soapVersion(SOAP_1_2)
->basicAuth(config('services.weather.user'), config('services.weather.password'))
->connectionTimeout(10)
->cache(WSDL_CACHE_BOTH);
});
}
use Siberfx\Soap\SoapWrapper;
public function transfer(SoapWrapper $soap, array $payload): mixed
{
$result = $soap->call('Bank.Transfer', [$payload], outputHeaders: $headers);
$sessionId = $headers['SessionId'] ?? null;
return $result;
}
namespace App\Soap\Types;
class GetConversionAmount
{
public function __construct(
public string $CurrencyFrom,
public string $CurrencyTo,
public string $RateDate,
public string $Amount,
) {}
}
class GetConversionAmountResponse
{
public string $GetConversionAmountResult;
}
Soap::call('Secure.GetOrders', [$request], inputHeaders: [
new SoapHeader('http://example.com/tracing', 'CorrelationId', (string) Str::uuid()),
]);
$client = Soap::client('Currency');
$client->functions(); // operations described by the WSDL
$client->types(); // types described by the WSDL
$client->call('GetCurrencies');
$client->GetCurrencies(); // native magic calls work too
use Siberfx\Soap\Exceptions\SoapCallException;
try {
Soap::call('Bank.Transfer', [$payload]);
} catch (SoapCallException $e) {
$e->service; // "Bank"
$e->method; // "Transfer"
$e->faultCode(); // e.g. "soap:Server"
$e->faultString(); // human readable message from the server
$e->detail(); // the <detail> element, if any
$e->lastRequest; // raw XML (trace enabled)
$e->lastResponse; // raw XML (trace enabled)
$e->fault; // the original \SoapFault (also getPrevious())
}
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Siberfx\Soap\Events\SoapRequestFailed;
use Siberfx\Soap\Events\SoapResponseReceived;
Event::listen(function (SoapResponseReceived $event) {
if ($event->duration > 2000) {
Log::warning("Slow SOAP call {$event->service}.{$event->method}", ['ms' => $event->duration]);
}
});
Event::listen(function (SoapRequestFailed $event) {
Log::error($event->exception->getMessage(), [
'request' => $event->exception->lastRequest,
'response' => $event->exception->lastResponse,
]);
});
use Siberfx\Soap\Facades\Soap;
use Siberfx\Soap\Testing\RecordedCall;
public function test_it_converts_currency(): void
{
Soap::fake([
'Currency.GetConversionAmount' => (object) ['GetConversionAmountResult' => '921.50'],
]);
$this->post('/convert', ['from' => 'USD', 'to' => 'EUR', 'amount' => 1000])
->assertOk()
->assertSee('921.50');
Soap::assertCalled('Currency.GetConversionAmount', fn (RecordedCall $call) =>
$call->argument('0.CurrencyFrom') === 'USD'
);
}
Soap::fake([
'Weather.Forecast' => fn (RecordedCall $call) => "Sunny in {$call->argument('0.city')}",
'Bank.Transfer' => new SoapFault('Server', 'Insufficient funds'),
'Currency.*' => 1.08,
]);