I need to pass a value from PHP to C++. I think I can do with PHP's passthru()
function.
Then I want C++ to do something to that value and return the result to PHP.
This is the bit I can't work out, does anyone know how to pass data from C++ to PHP?
I'd rather not use an intermediate file as I am thinking this will slow things down.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- Sorting 3 numbers without branching [closed]
- PHP Recursively File Folder Scan Sorted by Modific
- How to compile C++ code in GDB?
You could have your c++ app send its output to stdout, then call it from PHP with backticks, e.g.
wow thanks a lot to Columbo & Paul Dixon.
Now I can run php to call c++ thn pass back value to php~ =)
Here I provide a sample cpp & php for it:
a.cpp (a.exe):
sample.php:
output:
If you need to communicate to a running C++ program, a socket connection over the localhost might be the simplest, most platform-independent and most widely supported solution for communicating between the two processes. Sockets were traditionally built to communicate over network interfaces, but I have seen them used in many cases as a method of doing a pipe. They are fairly ubiquitous and supported in most modern languages.
Here's a C guide for socket programming. for your C++ program.
This article about wrapping C++ classes in a PHP extension may help.
EDIT: all the solutions in other answers are far simpler, but less flexible. It all depends on what you need.