tags:

views:

95

answers:

1

Hi There,

Does anyone know how to read stdin using a custom Command in the Yii framework?

I am busy writing a script to process incoming mails through a php script but need functionality from within the Yii framework as well. By default Yii passes the command line arguments in a variable to your run() method when you extend the CConsoleCommand. Any ideas?

A: 

Ok this is going to be funny I know, but after posting my question I also figured it out.

For those of you using the Super cool Yii framework, and want to do something like this, here is how you would go about doing that.

In your public run() method just add the following:

$fd = fopen("php://stdin", "r");
$content = "";
while (!feof($fd)){
   $content .= fread($fd, 1024);
}
fclose($fd);

you would then be able to get whatever was sent / streamed to the file in the $content variable.

Any code you add after this block would be processed as soon as the stdin was captured.

c'',)

Conrad
You don't need to open stdin, the PHP CLI SAPI does that automatically for you, putting the same "$fd" you've got into the constant STDIN. Yii also takes care of STDIN being declared.
Flavius