I know this is a very old issue but I see bits and pieces and it s difficult to piece it all together.
Expanding on Stefan Gehrig answer, I ended up having to extend SoapClient in order to be able to use the dynamic methods from the WSDL. Also, out of convenience, this class adds the tracing option directly in the construct.
<?php
class SoapClientLogger extends SoapClient
{
/**
* @var string
*/
protected $provider;
/**
* Create the SoapClient instance.
*
* @param string|null $wsdl
* @param array|null $options
* @throws SoapFault
*/
public function __construct(string $wsdl = null, ?array $options = null)
{
/**
* Set trace option to enable logging.
*/
$options = array_merge($options, [
trace => 1,
]);
parent::__construct($wsdl, $options);
}
/**
* Overloading __doRequest method.
*
* @param string $request
* @param string $location
* @param string $action
* @param int $version
* @param null $one_way
* @return string|null
*/
public function __doRequest($request, $location, $action, $version, $one_way = NULL): ?string
{
$sentAt = now();
$startTime = microtime(true);
$response = parent::__doRequest($request, $location, $action, $version, $one_way);
$this->log(
$location,
$action,
$version,
$sentAt,
number_format(microtime(true) - $startTime, 4),
parent::__getLastRequestHeaders(),
parent::__getLastRequest(),
parent::__getLastResponseHeaders(),
// Sometimes get last response is null but $response has the body.
parent::__getLastResponse() ?? $response
);
return $response;
}
/**
* Handle logging.
*
* @param string $location
* @param string $action
* @param string $version
* @param Carbon $sentAt
* @param float $timing
* @param string|null $requestHeaders
* @param string|null $requestBody
* @param string|null $responseHeaders
* @param string|null $responseBody
*/
protected function log(
string $location,
string $action,
string $version,
Carbon $sentAt,
float $timing,
string $requestHeaders = null,
string $requestBody = null,
string $responseHeaders = null,
string $responseBody = null
) {
// Do logging tasks here.
}
}
Hope this helps provide clarity on possible solutions.