在Kohana之外的文件中使用Kohana

Here's the situation:

I have a catch-all on my domain email (so *@domain.com) redirecting to a piping script located at /home/domain/scripts/piper.php. This piper script is not within the Kohana ORM, but all of my other files are. I want to try to use Kohana inside this piper.php file.

I have tried (unsuccessfully) all of the following:

Including Kohana

I couldn't figure out what needed to be included, and more importantly how to override the url variable that Kohana uses to determine the right controller. Also, this is a catch-all piper, so it isn't using HTTP (to my knowledge), so much as executing a command.

Piping

I tried piping to the following:

/home/domain/public_html/index.php --uri="piper"

But cPanel makes this impossible, as you can only specify the destination script, and not the proper flags and such (unless I am missing something).

PHP exec()

I tried using the following line:

exec("php /home/domain/public_html/index.php --uri=\"/piper\"")

I was hoping that the stdin data would be maintained across the exec() command, but I could never get it to recognize the uri command, though I can run this on my localhost and it works just fine.

I was using http://www.coderelic.com/2011/10/creating-cron-jobs-in-kohana-3-x-is-a-piece-of-cake/ as a reference, but can't get anything to work.

I'm happy with either one of these solutions such that I can see an incoming email, parse it, then send emails based on the parameters.

Let me know if you need more information! I'm le stumped.

All you need to do is to:

  1. modify your piper.php script to be a valid PHP class;
  2. place it in /application/classes/ folder;

Kohana will automatically load your class file (like include) during initialization.

Then you can use your piper class as usual class by $piper = new Piper; ....

UPD

You have to serve your emails trough Kohana.

Create controller, for example pipe (route it with /pipe URL):

public function action_pipe() {

    $pipe = new Pipe; // This creates new Pipe object (your emails serving class)

    $pipe->serve(); // Sserve emails within `serve()` method of Pipe class

}

/home/domain/public_html/index.php --uri="piper" would be a valid way to do it. If your host sucks and doesn't let you specify that, put it into a bash script instead and reference that.

If you are on any recent version of kohana (3.2 or 3.3), a better way to do this would be to use Minion to run the command line task. This is what Minion was designed for.

Although admittedly, I'm not sure if these other answers are correct because I can't figure out how to reproduce the results.

What ended up working for my situation was to create a Controller_Piper class that is called in the /home/domain/scripts/piper.php. What I did was to copy the code from /home/domain/public_html/index.php and changed the following:

echo Request::factory("/piper")
->execute()
->send_headers(TRUE)
->body();

This loads the piper controller and executes everything very nicely. Not sure if it's the cleanest, but it does work.