我想要交流PHP和C++代码。
我需要在他们之间传递一个大的JSON。问题是,我目前使用的是"passthru",但出于某种原因,我不知道C ++代码没有接收整个参数,而是在JSON为3156时被剪切成528个字符。
通过执行测试,我能够验证"passthru“命令支持的字符多达3156个。但我不知道C ++中是否存在最大输入参数大小。
PHP应用程序如下:
passthru('programc++.exe '.$bigJSON, $returnVal);
C++应用程序:
int main(int argc, char* argv[]){
char *json = argv[1];
}
有什么办法解决这个问题吗?我读过关于PHP扩展和IPC协议的文章,但问题是我必须做一个多平台程序(我必须有一个Windows版本,另一个版本用于Linux和Mac)。我认为使用PHP扩展和IPC协议(据我所知)会使事情变得相当复杂。
发布于 2017-06-15 03:24:54
解决方案:解决方案是使用"proc_open“,并使用管道stdin和stdout。在我的例子中,我使用图书馆的rapidjson。为了使rapidJSON工作并处理JSON,我在PHP中添加了双引号。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
}
https://stackoverflow.com/questions/44565491
复制相似问题