简体   繁体   English

PHP和C ++之间的通信

[英]Communication between PHP and C++

I want to communicate PHP and C++ code. 我想传达PHP和C ++代码。

I need to pass a big JSON between them. 我需要在它们之间传递一个大的JSON。 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. 问题是我目前正在使用“passthru”,但由于某种原因我不知道,C ++代码没有收到整个参数,但是当JSON为3156时,它被剪切为528个字符。

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 ++. 通过执行测试,我已经能够验证“passthru”命令支持与3156一样多的字符。但我不知道C ++中是否存在最大输入参数大小。

The PHP application is as follows: PHP应用程序如下:

passthru('programc++.exe '.$bigJSON, $returnVal);

The C++ application: C ++应用程序:

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). 我已经阅读了关于PHP扩展和IPC协议的内容,但问题是我必须做一个多平台程序(我必须有一个适用于Windows的版本,另一个适用于Linux和Mac)。 And I think that using PHP extensions and IPC protocols (as far as I could read) complicates things quite a bit. 而且我认为使用PHP扩展和IPC协议(据我所知)会使事情变得复杂。

Solution: The solution is use "proc_open" and use the pipe stdin and stdout. 解决方案:解决方案是使用“proc_open”并使用管道标准输入和标准输出。 In my case, I use the library rapidjson. 在我的例子中,我使用库rapidjson。 I add double quotes in PHP in order to rapidJSON works and process the JSON. 我在PHP中添加双引号,以便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++: C ++:

int main(int argc, char* argv[]){
    std::string json;
    std::getline (std::cin, json);
    cout << json << endl; // The JSON
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM