您可以捕获 zf2 应用程序在执行期间发出的所有 curl 请求,并使用 CURLOPT_VERBOSE 将它们记录下来,并使用 CURLOPT_WRITEHEADER 或 curl_getinfo() 将其写入一个日志文件,然后像这样读取它以在 ZF 开发人员工具栏上显示它。 .
准备具有 CURLOPT_VERBOSE 和 CURLOPT_WRITEHEADER 选项的 curl 请求,如下所示。
function curl_request($url, $log_file_path) {
//1. Prepare log file to append request details in to this log file
$logfile_fp = fopen($log_file_path, "a+");
//2. Prepare curl request to having CURLOPT_VERBOSE and CURLOPT_WRITEHEADER parameters in it
$request = new Request();
$request->setUri($url);
$request->setMethod('POST');
$client = new Client();
$adapter = new \Zend\Http\Client\Adapter\Curl();
$client->setAdapter($adapter);
$adapter->setOptions(array(
'curloptions' => array(
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_VERBOSE => 1,
CURLOPT_WRITEHEADER => $logfile_fp,
// Your curl request options here...
// Your curl request options here...
// Your curl request options here...
// Your curl request options here...
// Your curl request options here...
)
));
//3. Execute curl request
$response = $client->dispatch($request);
//4. Get curl request info
$handle = $client->getAdapter()->getHandle();
$request_info = curl_getinfo($handle);
//5. Write curl request info into log file
@fwrite($logfile_fp, implode(",", $request_info);
@fclose($logfile_fp);
}
说明:
- 准备日志文件以将请求详细信息附加到此日志文件中。
- 准备 curl 请求以在其中包含 CURLOPT_VERBOSE 和 CURLOPT_WRITEHEADER 参数。
- 执行 curl 请求。
- 使用 curl_getinfo() 获取 curl 请求信息。
- 将 curl 请求信息写入日志文件
在此之后,您可以使用 zend 文件阅读器或 fread() 读取日志文件以将其显示在开发人员工具栏上。
或
除此之外,还有其他第三方解决方法可以使用 netstat 或 tcpdump 或 wireshark 跟踪您的服务器流量,如下所示。
您可以使用 netstat。例如:
$ netstat -nputw
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 192.168.2.48:60614 151.101.65.69:80 ESTABLISHED 2527/chrome
tcp 0 0 192.168.2.48:58317 198.252.206.25:443 ESTABLISHED 2527/chrome
阅读netstat's 手册页了解更多详情。
或
您可以在 apache 之外的服务器上使用 tcpdump 工具来跟踪所有网络流量,例如:
$ tcpdump -vv -s0 tcp port 80 -w /tmp/apache_outgoing.pcap
阅读tcpdump's 手册页了解更多详情。