PHP syntax error while reading STDIN

I am creating a CLI script in PHP that needs to read content from STDIN and process it in a variable. It is executable, but the result is the same call as the argument for the "php" command.

This is the script that is triggered by the WHM event hook.

Here is the file:

#!/usr/bin/php -q

<?php

$switches = (count($argv) > 1) ? $argv : array();

if (in_array('--describe', $switches)) {
    echo json_encode(describe());
    exit;
} elseif (in_array('--debug', $switches)) {
    list($status, $msg) = debug();
    echo "$status $msg";
    exit;
} else {
    echo '0 myapp/whm.php needs a valid switch';
    exit(1);
}


function getStdInStream()
{
    $input   = fopen('php://stdin', 'r');
    $rawData = '';

    if (is_resource($input)) {
        stream_set_blocking($input, 0);

        while (($line = fgets($input, 1024)) !== false) {
            $rawData .= trim($line);
        }

        fclose($input);
    }

    if ($rawData) {
        $data = json_decode($rawData, true);
    } else {
        $data = array('context'=>array(), 'data'=>array(), 'hook'=>array());
    }

    return $data;
}

function describe()
{
    $debug = array(
        'category' => 'Cpanel',
        'event'    => 'UAPI::Branding::get_application_information',
        'stage'    => 'pre',
        'hook'     => '/usr/local/cpanel/3rdparty/bin/hooktest --debug',
        'exectype' => 'script',
    );
    return array($debug);
}

function debug()
{
    $status = 1;
    $msg = 'Triggered!';

    $input = getStdInStream();

    return array($status, $msg);
}

      

Running this on the command line returns a syntax error on line 24:

$ /usr/local/cpanel/3rdparty/bin/hooktest
<br />
<b>Parse error</b>:  syntax error, unexpected '{' in  <b>/usr/local/cpanel/3rdparty/bin/hooktest</b> on line <b>24</b><br />

      

I don't see anything wrong with the syntax. I did some tests like commented out the code inside the function getStdInStream

and the error went away.

That the script is based on an example found here: https://documentation.cpanel.net/display/SDK/Guide+to+Standardized+Hooks+-+Script+Hook+Action+Code

Tried commenting and uncommenting line by line and nothing works after execution fopen

.

Do you have any idea why I can't read STDIN using this approach or if it's a PHP bug?

My PHP version:

PHP 5.4.41 (cgi-fcgi) (built: May 22 2015 16:12:44)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies

      

+3


source to share


1 answer


Just found what seems like a solution. Changing hashbang from #!/usr/bin/php -q

to #!/usr/bin/env php

caused PHP to interpret the script differently.

I noticed that using the original script (from the question) global constants STDIN

and are STDOUT

not defined. Using it #!/usr/bin/env php

also solves this problem.



According to the manpages for, env

it runs the program in a modified environment instead of "just" passing the script to the interpreter (php in this case). What makes me think that it is correctly forwards STDIN

and STDOUT

to the code in the script environment.

0


source







All Articles