PHP require() 或 include() 停止脚本没有错误

PHP require() or include() stops the script without errors

我有这个代码:

echo "1 - is_readable: " . is_readable("/var/www/docroot/wp-load.php") . "\r\n";
echo "2 - file_exists: " . file_exists("/var/www/docroot/wp-load.php") . "\r\n";
echo "3 - before require\r\n";
require("/var/www/docroot/wp-load.php");
echo "4 - after require\r\n";

但是输出很奇怪:

1 - is_readable: 1
2 - file_exists: 1
3 - before require

This situation appears when I start script from CLI or CRON, when I start it directly in browser - all is fine.

echo 4 不显示是怎么回事? 我也试过要求另一个文件,结果是一样的。

更新。 crontab 中的任务:

*/1 * * * * root php -f /mypath/fetch_data.php >> /mypath/results.out.log 2>&1

如果您查看 wp-load.php 文件。它有下面的行

error_reporting( E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR );

这基本上会禁用您想要的所有错误报告。所以编辑文件并评论它并添加

error_reporting(E_ALL);

那你就知道是什么问题了。此外,并非所有 php 文件都与 CLI 兼容,因为它们使用的一些代码可能仅在 运行 在网络环境下有效。

因此,如果您无法修复 CLI 中的错误,您可以 运行 curl 调用来执行迁移

*/1 * * * * root curl http://localhost/path/to/url

问题在于您不会获得日志,而只会获得脚本提供的输出。此外,如果您的脚本 运行s 很长,那么您将需要添加 set_time_limit(0)

https://www.php.net/manual/en/function.set-time-limit.php

该脚本可能已经包含在内,并且在第二次包含时产生错误。请尝试 require_once()https://www.php.net/manual/en/function.require.php https://www.php.net/manual/en/function.require-once.php