如何记录Zend_Http_Client请求


How to Log the Zend_Http_Client request

我试图在执行请求之前将其记录下来,以真正了解我要发送的内容。例如:我有这个代码:

$client = new Zend_Http_Client();
$client->setUri($uri);
$client->setConfig(array('timeout' => 30));
$client->setHeaders('Content-Type: application/xml');
$client->setMethod('POST');
$client->setParameterPost('PartnerID', 'xxx');
$client->setParameterPost('Password', 'XXXXXXXXX');

在我做请求之前,我想知道我在发送什么,类似于

$request = json_encode($client);
Log::notice("Request: " . $request);

或:

Log::notice("Request: " . $client);

但不起作用。。。

我可以这样记录响应:

$response = $client->request();
Log::notice("Response: " . $response);

像这样,我可以看到响应json,但我想知道我正在做的请求。

谢谢大家。

您使用函数Zend_Http_Client->getLastRequest()获得最后一个请求。请注意,函数返回一个字符串,您可能需要添加一些代码才能将其放入有意义的JSON输出中。将以下内容添加到您的代码中:

$request = $client->getLastRequest()
// make your changes to support JSON
Log::notice("Request: " . $request)

在线文档Introduction-Zend_Http-Zend-Framework-Accessing Last Request and Response中也提到了该函数。