views:

503

answers:

3

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

+1  A: 

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.

Pekka
It's weird I'd have to change this though, as it *seems* it has support for CLI (having a static property of the Kohana class `$is_cli`) http://github.com/kohana/core/blob/master/classes/kohana/core.php#L54 I'm going to post to the Kohana forums as well. Thanks a lot for your answer.
alex
@alex it may be just an oversight. `E_STRICT` shouldn't fire at all in a production environment. (Of course, the much cleaner way would be to check for HTTP_HOST first.)
Pekka
Actually I can turn off errors in the Kohana config. I just want to be able to test my CRON from here without turning errors off. I suppose I can just do that. Thanks Pekka.
alex
@alex you're welcome. If you can influence the bootstrap, you can switch errors on/off depending on whether you're running in a cron job using php_sapi_name: http://de.php.net/manual/en/function.php-sapi-name.php
Pekka
+2  A: 

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://.

D. Evans
A: 

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

twicejr
I think if I do that though, then I can't test for `cli` in `PHP_SAPI` which means it could be accessed externally (unless I also check for host IP).
alex