ZeroPHP

SOAP Client

SOAP Client

Http::soap() returns a fluent SOAP request builder. Built on ext-soap when loaded; falls back to a minimal cURL-based envelope sender when it isn't.

use Zero\Lib\Http;

// WSDL mode
$r = Http::soap('https://api.example.com/service?wsdl')
    ->withBasicAuth($user, $pass)
    ->call('GetRates', ['currency' => 'USD']);

// Non-WSDL mode
$r = Http::soap()
    ->endpoint('https://example.com/endpoint')
    ->uri('urn:example:service')
    ->action('urn:example:service#GetRates')
    ->call('GetRates', ['currency' => 'USD']);

// Magic method shortcut
$user = Http::soap()->endpoint($url)->uri($ns)->GetUser([42])->result();

Implementation: SoapRequest.php and SoapResponse.php.


Building the request

Http::soap(?string $wsdl = null): SoapRequest

Entry point. Pass a WSDL URL/path, or omit and use endpoint() + uri().

->wsdl(string $url): self

Http::soap()->wsdl('https://example.com/svc?wsdl')->call('Foo');

->endpoint(string $url): self

Http::soap()->endpoint('https://example.com/svc')->uri('urn:foo')->call('Bar');

->uri(string $namespace): self

Target namespace for non-WSDL mode.

Http::soap()->endpoint($url)->uri('urn:example:service');

->noWsdl(): self

Force non-WSDL mode (drops any previously set WSDL).

->action(string $soapAction): self

Sets the SOAPAction HTTP header.

Http::soap()->endpoint($url)->uri($ns)->action('urn:Foo#Bar')->call('Bar');

->version(int $version): self

SOAP_1_1 (default) or SOAP_1_2.

Http::soap()->endpoint($url)->uri($ns)->version(SOAP_1_2)->call('Foo');

->style(int $style): self

SOAP_RPC or SOAP_DOCUMENT.

->use(int $use): self

SOAP_LITERAL or SOAP_ENCODED.

->encoding(string $encoding): self

Http::soap()->encoding('UTF-8')->call('Foo');

->timeout(int $seconds): self

Connection timeout.

Http::soap('...')->timeout(10)->call('Foo');

->withWsdlCache(int $mode): self

WSDL_CACHE_NONE, WSDL_CACHE_DISK, WSDL_CACHE_MEMORY, WSDL_CACHE_BOTH.

Http::soap('...')->withWsdlCache(WSDL_CACHE_DISK);

->withBasicAuth(string $username, string $password): self

Http::soap('...')->withBasicAuth('user', 'pass')->call('Foo');

->withDigestAuth(string $username, string $password): self

Http::soap('...')->withDigestAuth('user', 'pass');

->withClientCertificate(string $path, ?string $passphrase = null): self

Http::soap('...')->withClientCertificate('/etc/ssl/client.pem', 'secret');

->withProxy(string $host, int $port, ?string $login = null, ?string $password = null): self

Http::soap('...')->withProxy('proxy.local', 3128);

->withUserAgent(string $userAgent): self

Http::soap('...')->withUserAgent('ZeroSoap/1.0');

->compression(int $flags): self

SOAP_COMPRESSION_*.

Http::soap('...')->compression(SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP);

->keepAlive(bool $keepAlive = true): self

Http::soap('...')->keepAlive();

->withClassMap(array $classMap): self

Map WSDL types to PHP classes.

Http::soap('...')->withClassMap(['User' => App\Soap\User::class]);

->withTypeMap(array $typeMap): self

Custom (de)serialization hooks for specific XSD types.

->withFeatures(int $flags): self

SOAP_* feature flags.

->withStreamContext($ctx): self

Custom stream context.

->withOptions(array $options): self

Pass any other SoapClient option directly.

->withSoapHeader(string $name, string $namespace, mixed $data = null, bool $mustUnderstand = false, ?string $actor = null): self

Add a SOAP header.

Http::soap('...')
    ->withSoapHeader('Auth', 'urn:zero', ['token' => $tok])
    ->call('Foo');

->trace(bool $trace = true): self

Capture last request/response on the response object.

$r = Http::soap('...')->trace()->call('Foo');
$r->lastRequest();

->throw(bool $throw = true): self

Throw SoapFault on failure.

Http::soap('...')->throw()->call('Foo'); // throws on fault

->withClient(string $class): self

Use a custom SoapClient subclass (must extend SoapClient).

Http::soap('...')->withClient(MyClient::class);

Calling

->call(string $method, array $arguments = []): SoapResponse

$r = Http::soap('...')->call('Add', [2, 3]);
$r->result(); // 5

Magic call: ->$method($args)

$r = Http::soap()->endpoint($url)->uri($ns)->Add([10, 20]);
$r->result(); // 30

Introspection (requires ext-soap + WSDL)

->client(): SoapClient

$client = Http::soap('...')->client();

->functions(): array<int, string>

Http::soap('...')->functions(); // ['Add(int $a, int $b) Add', ...]

->types(): array<int, string>

Http::soap('...')->types();     // ['struct User { ... }', ...]

Inspecting the response

Implementation: SoapResponse.php.

result(): mixed

The decoded service result.

Http::soap('...')->call('Add', [2, 3])->result(); // 5

get(?string $key = null, $default = null): mixed

Dot-notation lookup (works on arrays and objects).

$r = Http::soap('...')->call('GetUser', [42]);
$r->get('id');         // 42
$r->get('meta.level'); // 7

toArray(): array

Recursively normalize objects to arrays.

Http::soap('...')->call('GetUser', [42])->toArray();

headers(): array

SOAP response headers.

fault(): ?SoapFault / successful(): bool / failed(): bool

$r = Http::soap('...')->call('Boom');
if ($r->failed()) {
    $fault = $r->fault();
}

lastRequest(): ?string / lastRequestHeaders(): ?string

Available when trace() was set.

lastResponse(): ?string / lastResponseHeaders(): ?string

Available when trace() was set.

throw(): self

Throw the fault (if any), else return $this.

Http::soap('...')->call('Foo')->throw();

On this page

SOAP ClientBuilding the requestHttp::soap(?string $wsdl = null): SoapRequest->wsdl(string $url): self->endpoint(string $url): self->uri(string $namespace): self->noWsdl(): self->action(string $soapAction): self->version(int $version): self->style(int $style): self->use(int $use): self->encoding(string $encoding): self->timeout(int $seconds): self->withWsdlCache(int $mode): self->withBasicAuth(string $username, string $password): self->withDigestAuth(string $username, string $password): self->withClientCertificate(string $path, ?string $passphrase = null): self->withProxy(string $host, int $port, ?string $login = null, ?string $password = null): self->withUserAgent(string $userAgent): self->compression(int $flags): self->keepAlive(bool $keepAlive = true): self->withClassMap(array $classMap): self->withTypeMap(array $typeMap): self->withFeatures(int $flags): self->withStreamContext($ctx): self->withOptions(array $options): self->withSoapHeader(string $name, string $namespace, mixed $data = null, bool $mustUnderstand = false, ?string $actor = null): self->trace(bool $trace = true): self->throw(bool $throw = true): self->withClient(string $class): selfCalling->call(string $method, array $arguments = []): SoapResponseMagic call: ->$method($args)Introspection (requires ext-soap + WSDL)->client(): SoapClient->functions(): array<int, string>->types(): array<int, string>Inspecting the responseresult(): mixedget(?string $key = null, $default = null): mixedtoArray(): arrayheaders(): arrayfault(): ?SoapFault / successful(): bool / failed(): boollastRequest(): ?string / lastRequestHeaders(): ?stringlastResponse(): ?string / lastResponseHeaders(): ?stringthrow(): self