我知道这是一个非常古老的问题,但我看到了零碎的东西,很难将它们拼凑在一起。
扩展Stefan Gehrig answer,我最终不得不扩展SoapClient 以便能够使用来自WSDL 的动态方法。此外,为方便起见,此类直接在构造中添加了跟踪选项。
<?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.
}
}
希望这有助于明确可能的解决方案。