有关 Kohana 3 和 CRON 的帮助

发布于 2024-08-26 06:35:29 字数 953 浏览 4 评论 0原文

我已经设置了一个 CRON 来调用 Kohana 3 中的 URL。

php /home/user/public_html/index.php my/route/in/bootstrap.php

它似乎可以很好地访问该 URL。

但是,我收到了此错误(通过我的主机根据 CRON 发送的电子邮件发回)

Undefined index:  HTTP_HOST
SYSPATH/classes/kohana/url.php [ 40 ]

url.php 的来源

位于 Kohana 系统文件中。这是因为 CRON 作业没有发送 HTTP 标头吗?

我将如何解决这个问题并使其正常工作(希望不要破解核心文件)。

或者我做的CRON错了?

更新

Pekka 提供了很好的答案,但是我'我想避免更改核心文件(尽管我会作为最后的手段)。

Kohana 3 似乎确实支持 CLI,因为有一个静态属性 $is_cli

http://github.com/kohana/core/ blob/master/classes/kohana/core.php#L54

I've set up a CRON to call a URL in Kohana 3.

php /home/user/public_html/index.php my/route/in/bootstrap.php

It seems to access the URL fine.

However, I have received this error (send back in an email that my host sends per CRON)

Undefined index:  HTTP_HOST
SYSPATH/classes/kohana/url.php [ 40 ]

Source of url.php

Which is in a Kohana system file. Is this because the CRON job is not sending HTTP headers?

How would I fix this and get it to work (hopefully without hacking the core files).

Or am I doing the CRON wrong?

Update

Pekka provided a good answer, however I'd like to avoid changing the core files (though I will as a last resort).

It would seem Kohana 3 does have support for CLI, as there is a static property $is_cli.

http://github.com/kohana/core/blob/master/classes/kohana/core.php#L54

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

戏蝶舞 2024-09-02 06:35:29

看起来您已打开 E_STRICT 通知,并且 Kohana 的错误处理捕获了该通知。 E_STRICT 会抱怨未定义的索引。该索引确实未定义,因为通过 CLI 调用时 PHP 脚本中没有 HTTP_HOST。

尽管如此,您的脚本很可能运行良好。您必须在某些时候关闭 error_reporting 以防止显示该消息 - 我不太了解 Kohana,不知道从 CLI 调用时是否可以使用不同的配置文件。

也许只需关闭特定控制器中的 error_reporting() 就可以解决问题,尽管这有点hacky。

It seems like you have E_STRICT notification turned on, and Kohana's error handling catches that. E_STRICT will complain about undefined indexes. The index is indeed undefined because there is no HTTP_HOST in a PHP script when called through the CLI.

Chances are your script is running fine despite this. You would have to turn down error_reporting at some point to prevent the message from showing up - I don't know Kohana well enough to know whether you can use a different config file when called from the CLI.

Maybe just turning down the error_reporting() in your specific controller does the trick, although it's a bit hacky.

情愿 2024-09-02 06:35:29

作为一般政策,我建议不要仅仅降低错误报告级别来消除错误。

问题在于,正如 Pekka 所说,$_SERVER['HTTP_HOST'] 没有在 CLI 模式下定义,而 Url 类在生成绝对 URL 时需要它。在很多情况下都会发生这种情况,例如在设置了 $protocol 的情况下调用 URL::site 时,或者使用 Request::redirect() 时code>,或使用 Feed 帮助程序生成 RSS feed 时。

您需要做的是弄清楚您要在 CRONed 控制器中的哪个位置尝试生成绝对 URL,然后决定是否需要。如果您不需要它,请删除有问题的代码,它应该可以正常运行。如果您这样做,那么仅仅关闭错误对您没有帮助。相反,请将其添加到您的 bootstrap.php 文件中:

if ( ! isset($_SERVER['HTTP_HOST'])
{
    $_SERVER['HTTP_HOST'] = '<your-domain-here>';
}

您还需要确保将所需的协议(大概是“http”)显式传递给 URL::base 而不是仅仅传递 TRUE。否则它将使用当前协议,即 cli://

As a general policy, I'd advise against just turning down error reporting levels to make errors go away.

The problem is that, as Pekka says, $_SERVER['HTTP_HOST'] isn't defined in CLI mode and the Url class needs this to when generating absolute URLs. This can happen in quite a few circumstances, for example when calling URL::site with $protocol set, or when using Request::redirect(), or when generating an RSS feed using the Feed helper.

What you need to do is to work out where in your CRONed controller you are trying to generate an absolute URL, and then decide whether you need to be. If you don't need it, then remove the offending code and it should run fine. If you do, then just turning off errors won't help you. Instead, add this to your bootstrap.php file:

if ( ! isset($_SERVER['HTTP_HOST'])
{
    $_SERVER['HTTP_HOST'] = '<your-domain-here>';
}

You'll also need to make sure that you explicitly pass the protocol you want (presumably 'http') to URL::base rather than just passing TRUE. Otherwise it will use the current protocol which will be cli://.

不美如何 2024-09-02 06:35:29

另一个解决方案是像这样设置 cron 命令:

wget --timeout=99999 -O/dev/null -q http://localhost/kohana/url/and/some/segment/or/whatever

Another solution is to set the cron command like this:

wget --timeout=99999 -O/dev/null -q http://localhost/kohana/url/and/some/segment/or/whatever

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文