tmyonline Posted February 12, 2009 Share Posted February 12, 2009 Hi guys, Is there a way to interact PHP with bash so that the output of a bash program can be used as the input for a PHP program ? Thanks. Link to comment https://forums.phpfreaks.com/topic/145002-how-to-get-php-to-interact-with-bash-code/ Share on other sites More sharing options...
samshel Posted February 12, 2009 Share Posted February 12, 2009 you can call PHP script at the end of your bash code and pass whatever u want as command line parameter. if i understand u correctly.. Link to comment https://forums.phpfreaks.com/topic/145002-how-to-get-php-to-interact-with-bash-code/#findComment-760863 Share on other sites More sharing options...
corbin Posted February 12, 2009 Share Posted February 12, 2009 Do you mean piping? You could of course just use exec() inside of PHP, but if you want to pipe data into it, you could read php://stdin. $stdin = fopen('php://stdin', 'r'); echo fread($stdin, 1024); for example. Link to comment https://forums.phpfreaks.com/topic/145002-how-to-get-php-to-interact-with-bash-code/#findComment-760867 Share on other sites More sharing options...
samshel Posted February 12, 2009 Share Posted February 12, 2009 #!/bin/bash echo Hello World /usr/bin/php test.php parameter1 parameter2 [code] Link to comment https://forums.phpfreaks.com/topic/145002-how-to-get-php-to-interact-with-bash-code/#findComment-760872 Share on other sites More sharing options...
tmyonline Posted February 12, 2009 Author Share Posted February 12, 2009 Hi guys, thanks for the prompt response. I'm comfortable with PHP but I'm a newbie at bash. Samshel, in your example #!/bin/bash echo Hello World /usr/bin/php test.php parameter1 parameter2 you are passing two parameters, namely parameter1 and parameter2, to a PHP file called "test.php" located in the php directory under bin. Is that what you meant ? If so, if I run test.php, it will automatically output the results which made use of parameter1 and parameter2 as the inputs ? The point I'm trying to make sure here is that I don't have to run each program, bash & PHP, individually. So, if I run test.php and if it automatically executes the bash program for the output parameter1 and parameter2 which serve as the input for the test.php program, then that's great. But, is it the case though ? Many thanks. Hi corbin, yeah, piping could be another method to achieve this. Do you have the link to this material. I searched Google on php://stdin but it didn't point me to the right place. Many thanks. Link to comment https://forums.phpfreaks.com/topic/145002-how-to-get-php-to-interact-with-bash-code/#findComment-760895 Share on other sites More sharing options...
corbin Posted February 12, 2009 Share Posted February 12, 2009 http://php.net/wrappers.php php://stdin is just a stream wrapper. You would basically treat it like any other stream (like a file stream for example). As for parameters passed on the command line, you would use $_SERVER['argc'] and $_SERVER['argv'] (or you could use $argc and $argv). Link to comment https://forums.phpfreaks.com/topic/145002-how-to-get-php-to-interact-with-bash-code/#findComment-760919 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.