0

I am taking some input from a form. I have a c++ program that processes the input and produces a hash value from it. How can I transfer data between the PHP script that handles the form and the c++ program that processes the input?

2 Answers2

2

What you want to do is come up with a data interface between C++ and PHP. I recommend using JSON since PHP has the simple json_decode function.

You'll need to investigate a bit into how to save to JSON from C++, consider looking here.

Have your C++ program save what you want to use in PHP to a .json file then load the .json file in PHP with file_get_contents and json_decode. For example if you want your C++ program to generate a message you could save an object with something like { message : "Hello World" } and save it to message.json.

Then in PHP do:

$file_data = json_decode(file_get_contents("message.json"));
echo $file_data["message"];

EDIT: You could also have C++ save your data to a database and read from the database with PHP. That's simple(ish).

Community
  • 1
  • 1
user123498
  • 58
  • 1
  • 1
  • 5
0

Depends on what is the C++ application like:

  1. It is some sort of daemon, that continuously generates some data (some measurement or so). Then it can store those data to some file or database and PHP just reads and displays those data on demand.

  2. It is some sort of "service" daemon - it listens on some port and is able to communicate through a socket. Then You can connect to it from Your PHP script and ask for the data using Your application's protocol. See http://php.net/manual/en/sockets.examples.php

  3. It is a "simple" application that You need to execute from PHP and get it's output for displaying (or result parsing). Then You can either exec() it or run it through proc_open()

Roman Hocke
  • 4,137
  • 1
  • 20
  • 34
  • It is kind of a daemon which continuously generates some data. I want it to directly connect with php. Can I do that? – Tanmoy Krishna Das Jan 09 '15 at 18:06
  • What exactly do You mean by "directly connect with PHP"? Would You give an example of functionality, that You want to achieve? – Roman Hocke Jan 09 '15 at 20:47
  • I want php to get the data that users input and pass it to c++. Then when c++ completes the calculation, show the result back to the user. – Tanmoy Krishna Das Jan 10 '15 at 09:56
  • Then get user's input from some web form, use it to create shell command and run that command using `exec()`. It gives you command's output, so pass it back to the user. If the calculation is fast enough to do all this in one request. – Roman Hocke Jan 11 '15 at 23:35