Piping JSON object to PHP script
Created:03 Aug 2020 00:39:31 , in Host development
In this article I look at how to pass a string from a shell script to a PHP program. This problem shows up regularly to me when I make a HTTP request with curl on the command line and have to process response data returned by the utility, often JSON encoded data, using PHP.
Arguably, the quickest way to get access to data of HTTP response in a PHP script is to just carry out a HTTP request with PHP cURL wrapper. If it is done that way, the data is ready for processing as soon as it arrives. However, there are cases for which curl command line utility is a better option. Since HTTP request configurations for curl can be a tad complex in some cases, the command ends up in its own file and becomes a program which echoes HTTP response body. Now the problem is how to make the echoed string available to PHP.
Using system PHP function
There are a few ways to exchange data between a shell script and a program written in PHP. One of simpler ones is as follows:
$str = system('script.sh');
What happens here is, script.sh is run using PHP system(). The function returns data echoed by the script. Can't be any easier, can it?
Using pipe to pass data to PHP script
More involved option is to use a pipe to connect output of script.sh and input of script.php
$ script.sh | script.php
The above assumes that both script.sh and script.php are executable programs and living in a directory specified in the PATH environmental variable.
If script.sh returns a string with JSON object like this:
'{"item1":"value","item2":"value"}'
script.php can read and process the data as follows:
$handle = fopen('php://stdin','r');
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
$json_object = json_decode($contents);
This solution relies on PHP input stream wrapper called php://stdin. The wrapper can be read (it is read-only) in the same way as any ordinary file.
So, data is being read in chunks and until null character is encountered. When reading is finished, the stream gets closed and JSON encoded data gets turned to a PHP stdClass object with json_decode().
Conclusion
This article is about a very specific problem. One, which I could not find a clear solution to on php.net or elsewhere, so decided to write my own (three).
I hope the article will prove useful to you.
If you have something relevant to add, leave a comment below.
Author, Copyright and citation
Author
Author of the this article - Sylwester Wojnowski - is a sWWW web developer. He has been writing computer code for the websites and web applications since 1998.
Copyrights
©Copyright, 2024 Sylwester Wojnowski. This article may not be reproduced or published as a whole or in parts without permission from the author. If you share it, please give author credit and do not remove embedded links.
Computer code, if present in the article, is excluded from the above and licensed under GPLv3.
Citation
Cite this article as:
Wojnowski, Sylwester. "Piping JSON object to PHP script." From sWWW - Code For The Web . https://swww.com.pl//main/index/piping-json-object-to-php-script
Add Comment