PHP C + + veri geçirmeden

4 Cevap php

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.

Teşekkürler

4 Cevap

Eğer c + + app ardından backticks, örneğin PHP ile onu aramak, stdout çıkışını göndermek olabilir

$output=`myapp $myinputparams`;

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.

İşte C guide for socket programming., C + + için bir programdır.

This article about wrapping C++ classes in a PHP extension yardımcı olabilir.

EDIT: diğer yanıtlar tüm çözümleri çok basittir, ama daha az esnek. Her ne gerek bağlıdır.

vay sayesinde Columbo için bir çok & Paul Dixon.

Şimdi = ~ c + + thn php değer geri pas çağırmak için php çalıştırabilirsiniz) olabilir

Burada örnek bir cpp'nin sağlamak ve Bunun için php:

A simple program for adding up 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