Search code examples
phppipestdinphp-7

PHP: Pass file into script as stdin


I am attempting to write some tests for an email parser I am building and having trouble getting started.

For normal operation an email will be piped to the script, but for the tests I want to simulate the piping action : )

My test is starting out like this:

#!/opt/php70/bin/php
<?php

define('INC_ROOT', dirname(__DIR__));

$script = INC_ROOT . '/app/email_parser.php';

//$email = file_get_contents(INC_ROOT . '/tests/test_emails/test.email');
$email = INC_ROOT . '/tests/test_emails/test.email';

passthru("{$script}<<<{$email}");

With the script as is, the only thing passed to stdin is the path to the test email. When using file_get_contents I get:

sh: -c: line 0: syntax error near unexpected token '('
sh: -c: line 0: /myscriptpath/app/email_parser.php<<<TestEmailContents

Where TestEmailContents is the contents of the raw email file. I feel like I have executed scripts in this manner in the past using the heredoc operator to pass data into stdin. But for the last few days I have been unable to find any information to get me past this stumbling block. Any advice will be mucho appreciado!


Solution

  • The syntax error experienced was exactly that. To get the file contents and pass it in as a here string I needed to single quote the string:

    $email = file_get_contents(INC_ROOT . '/tests/test_emails/test.email');
    
    passthru("{$script} <<< '{$email}'");
    

    But, in my case passing in a raw email did not require the use of a here string. The line endings are preserved either way. Redirecting the file to the script yielded the same results.

    $email = INC_ROOT . '/tests/test_emails/test.email';
    
    passthru("{$script} < {$email}");