Transferring data from C ++ to PHP

I need to pass a value from PHP to C ++. I think I can do a function with PHP passthru() . Then I want C ++ to do something with this value and return the result in PHP. This is a bit that I can’t solve, does anyone know how to transfer data from C ++ to PHP? I would prefer not to use an intermediate file, as I think it will slow down.

+4
source share
4 answers

Maybe your C ++ application will send its output to stdout and then call it from PHP using backticks , for example

 $output=`myapp $myinputparams`; 
+6
source

Wow really thanks Colombo and Paul Dixon.

Now I can run php to call C ++ thn to pass back the php value ~ =)

Here I provide a sample cpp and php for it:

Simple program to add inputs:

a.cpp (a.exe):

 #include<iostream> #include<cstdlib> using namespace std; int main(int argc, char* argv[]) { int val[2]; for(int i = 1; i < argc; i++) { // retrieve the value from php val[i-1] = atoi(argv[i]); } int total = val[0] + val[1]; // sum up cout << total; // std::cout will output to php return 0; } 

sample.php:

 <?php $a = 2; $b = 3; $c_output=`a.exe $a $b`; // pass in the two value to the c++ prog echo "<pre>$c_output</pre>"; //received the sum echo "Output: " . ($output + 1); //modify the value in php and output ?> 

output:

 5 Output: 6 
+2
source

This article on wrapping C ++ classes in a PHP extension can help.

EDIT: all solutions in the other answers are much simpler but less flexible. It all depends on what you need.

0
source

If you need to contact a running C ++ program, connecting a socket to a local host may be the simplest, independent, and most common solution for exchanging data between two processes. Sockets have traditionally been built for communication over network interfaces, but I saw that they were used in many cases as a method of working with a pipe. They are fairly ubiquitous and are supported in most modern languages.

Here 's the socket programming guide. for your C ++ program.

0
source

Source: https://habr.com/ru/post/1285973/


All Articles