I want to communicate PHP and C++ code.
I need to pass a big JSON between them.
The problem is that I am currently using "passthru", but for some reason I do not know, the C ++ code does not receive the entire parameter, but is cut in 528 characters when the JSON is 3156.
By performing tests, I have been able to verify that the "passthru" command supports as many characters as 3156. But I do not know if there is a maximum input parameter size in C ++.
The PHP application is as follows:
passthru('programc++.exe '.$bigJSON, $returnVal);
The C++ application:
int main(int argc, char* argv[]){
char *json = argv[1];
}
Is there any way to fix the problem? I have read about PHP extensions and IPC protocols, but the problem is that I have to do a multiplatform program (I must have a version for Windows, another for Linux and for Mac). And I think that using PHP extensions and IPC protocols (as far as I could read) complicates things quite a bit.
Solution:
The solution is use "proc_open" and use the pipe stdin and stdout. In my case, I use the library rapidjson. I add double quotes in PHP in order to rapidJSON works and process the JSON.
PHP:
$exe_command = 'program.exe';
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout -> we use this
2 => array("pipe", "w") // stderr
);
$process = proc_open($exe_command, $descriptorspec, $pipes);
$returnValue = null;
if (is_resource($process)){
fwrite($pipes[0], $bigJSON);
fclose($pipes[0]);
$returnValue = stream_get_contents($pipes[1]);
fclose($pipes[1]);
}
C++:
int main(int argc, char* argv[]){
std::string json;
std::getline (std::cin, json);
cout << json << endl; // The JSON
}
Related
I have python script which may can ask for input while running (I must run it from PHP and can't modify python script). My question is, can I interact with that script with PHP. For example, run "python script.py" and then if last message is "String:", than insert input.
>> python script.py
>> Log 1 ...
>> Log 2 ...
>> Enter value A:
>> 2
>> Enter value B:
>> 4
i want to do this.
If(cmdlogtext == 'Enter value A:')
* Enter number 2
To interact with running script you need to use proc_open.
proc_open Official docs
Below example is a simple usage.
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", "error-output.txt", "a") // stderr is a file to write to
);
$process = proc_open('python script.py', $descriptorspec, $pipes);
if (is_resource($process)) {
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout
// Any error output will be appended to error-output.txt
fwrite($pipes[0], 'Input 1' . PHP_EOL);
fwrite($pipes[0], 'Input 2' . PHP_EOL);
echo stream_get_contents($pipes[1]);
fclose($pipes[0]);
fclose($pipes[1]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);
}
Keep in mind:
To send inputs to running script, you must use fwrite on $pipes[0].
All parameters needs to be ended by PHP_EOL
You can use the exec function and then inspect the output.
exec("python script.py", $output, $returnValue);
Edit: Re-read your question and I'm not sure about interactively providing input but you may be able to modify the exec() function to pass arguments to the Python script assuming the script can accept them.
Pngquant has the following example for php
// '-' makes it use stdout, required to save to $compressed_png_content variable
// '<' makes it read from the given file path
// escapeshellarg() makes this safe to use with any path
$compressed_png_content = shell_exec("pngquant --quality=$min_quality-$max_quality - < ".escapeshellarg( $path_to_png_file));
I want to replace $path_of_file with the actual content.
This will avoid wasting I/O when converting a file from one format to png and then optimize it
What will be the new shell_exec() command in that situation
I am no PHP expert, but I believe you are looking for a 2-way pipe (write and read) to another process, so that you can write data to its stdin and read data from its stdout. So, I think that means you need proc_open() which is described here.
It will look something like this (untested):
$cmd = 'pngquant --quality ... -';
$spec = array(array("pipe", "r"), array("pipe", "w"), array("pipe", "w"));
$process = proc_open($cmd, $spec, $pipes);
if (is_resource($process))
{
// write your data to $pipes[0] so that "pngquant" gets it
fclose($pipes[0]);
$result=stream_get_contents($pipes[1]);
fclose($pipes[1]);
proc_close($process);
}
I have one php script and I want to call a process writting in C from the PHP scrit. There are many ways to do it(system,exec...), but Ï chosse the function proc_open. With this I can open a pipe in stdin and stdout with the C process, but I don´t know how to get the data from stdin in the C process. Can anyone help me with a example?.Thank you
In C, stdin, stdout and stderr are constant FILE pointers defined in <stdio.h>. For example, to read from stdin:
#include <stdio.h>
int main() {
int ch = fgetc(stdin); //read 1 character from stdin
fputc(ch, stdout); //dump to stdout
//...
return 0;
}
I am creating a PHP file to pass values to a c++ .exe which will then calculate an output and return that output. However, I cannot seem to get the output from the .exe back into the PHP file.
PHP Code:
$path = 'C:enter code here\Users\sumit.exe';
$handle = popen($path,'w');
$write = fwrite($handle,"37");
pclose($handle);
C++ Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
// Declaation of Input Variables:
int main()
{
int num;
cin>> num;
std::cout<<num+5;
return 0;
}
I'd advise neither system nor popen but proc_open command: php.net
Call it like
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr, also a pipe the child will write to
);
proc_open('C:enter code here\Users\sumit.exe', $descriptorspec, $pipes);
After that you'll have $pipes filled with handles to send data to program ([0]) and recieve data from program ([1]). Also you will have [2] which you can use to get stderr from the program (or just close if you don't use stderr).
Don't forget to close process handle with proc_close() and pipe handles with fclose(). Note that your program will not know the output is complete before you close $pipes[0] handle or write some whitespace character. I advise closing the pipe.
Using command line arguments in system() or popen() is valid, though if you intend to send large amounts of data and/or raw data, you will have trouble with command line length limits and with escaping special chars.
In your C++ code I am not seeing anything for passing variables in you need
int main(int argc, char* argv[])
instead of
int main()
Remember argc is the count of variables and it includes the path to the file, so your arguments begin at 1 with each argv being a c-string of that argument. If you need a decimal atof is your friend or atoi for an integer.
Then you are using popen. The PHP documentation says that it can be only used for reading or writting. It is not bi-directional. You want to use proc_open to have bi-directional support.
Anyways, This is how I would write your C++ code:
#include "stdafx.h"
#include <iostream>
// Declaation of Input Variables:
int main(int arc, char* argv[])
{
int num;
num = atoi(argv[1]);
std::cout<<num+5;
return 0;
}
Note: I removed using namespace std because I noticed you were still trying to use the namespace in the main function (i.e. std::cout) and it better to keep it out of a global namespace.
you are writing into exe file, you should pass your argument like
system("C:enter code here\Users\sumit.exe 37");
I have a variable that contains a long string. (specifically it contains a few kilobytes of javascript-code)
I want to pass this string trough an external command, in this case a javascript-compressor, and capture the output of the external command (the compressed javascript) in php, assigning it to a variable.
I'm aware that there's classes for compressing javascript in php, but this is merely one example of a general problem.
originally we used:
$newvar = passthru("echo $oldvar | compressor");
This works for small strings, but is insecure. (if oldvar contains characters with special meaning to the shell, then anything could happen)
Escaping with escapeshellarg fixes that, but the solution breaks for longer strings, because of OS-limitations on maximum allowable argument-length.
I tried using popen("command" "w") and writing to the command - this works, but the output from the command silently disappears into the void.
Conceptually, I just want to do the equivalent of:
$newvar = external_command($oldvar);
Using the proc_open-function you can get handles to both stdout and stdin of the process and thus write your data to it and read the result.
Using rumpels suggestion, I was able to device the following solution which seems to work well. Posting it here for the benefit of anyone else interested in the question.
public static function extFilter($command, $content){
$fds = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a pipe that the child will write to
);
$process = proc_open($command, $fds, $pipes, NULL, NULL);
if (is_resource($process)) {
fwrite($pipes[0], $content);
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$return_value = proc_close($process);
// Do whatever you want to do with $stderr and the commands exit-code.
} else {
// Do whatever you want to do if the command fails to start
}
return $stdout;
}
There may be deadlock-issues: if the data you send is larger than the combined sizes of the pipes, then the external command will block, waiting for someone to read from it's stdout, while php is blocked, waiting for stdin to be read from to make room for more input.
Possibly PHP takes care of this issue somehow, but it's worth testing out if you plan to send (or receive) more data than fits in the pipes.