gpt4 book ai didi

php - (Linux CLI PHP) 在执行期间捕获 CURL 的详细输出到文件

转载 作者:IT王子 更新时间:2023-10-29 00:40:31 25 4
gpt4 key购买 nike

我刚刚注意到,如果您运行一个命令行 php 脚本,该脚本使用 curl 并启用了 CURLOPT_VERBOSE 选项,您将无法捕获输出...

例子:

$php myscript.php > logfile.txt 2>&1

所有 PHP 输出都将进入日志文件,但 curl 的输出仍会出现在屏幕上。

如何捕获 PHP 输出和 curl 输出?

    $ch = curl_init();  // Initialising cURL
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $this->agentString);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
curl_close($ch);

编辑:

添加行

curl_setopt($ch, CURLOPT_STDERR, STDOUT);

似乎可以解决问题。但是,现在看来,curl 输出与 php 的输出冲突,并用自己的输出覆盖了日志文件。奇怪!

编辑 2:

这行得通

 $php myscript.php >> logfile.txt 2>&1

最佳答案

其实我的第一个答案是错误的。 cURL 的手册页说:

URLOPT_VERBOSE TRUE to output verbose information. Writes output to STDERR, or the file specified using CURLOPT_STDERR.

因此为 CURLOPT_STDERR 指定 STDOUT 将起作用:

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, STDOUT);

原始答案:

检查您是否还设置了 CURLOPT_RETURNTRANSFER

关于php - (Linux CLI PHP) 在执行期间捕获 CURL 的详细输出到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26068910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com