简体   繁体   English

在c和php之间传递数据

[英]Passing data between c and php

I have two application - a php web application and c program. 我有两个应用程序 - 一个php Web应用程序和c程序。 The php app has to send data to the c program which returns results. php应用程序必须将数据发送到返回结果的c程序。 The c program has to run all the time in a cycle (while(true), because it has to hold data of previous input in memory. c程序必须在一个循环中一直运行(而(true)),因为它必须将先前输入的数据保存在内存中。

I handle the problem passing data between c and php by using files (input & output files). 我通过使用文件(输入和输出文件)处理在c和php之间传递数据的问题。 If there is a change in my web application my php script update a file, that is read by my c program. 如果我的web应用程序发生了变化,我的php脚本会更新一个由我的c程序读取的文件。 The output is written into an other file. 输出将写入另一个文件。 The web application is looking all the time for changes. Web应用程序一直在寻找变化。

I think it is not the smartest solution to solve a problem like this. 我认为解决这样的问题不是最聪明的解决方案。 Probaly its a lot not necessary file access. Probaly它有很多不必要的文件访问权限。

With inspiration and help from following thread i thought using a kind of c php server bridge could be a good solution. 从以下线程的灵感和帮助我认为使用一种c php服务器桥可能是一个很好的解决方案。

How to pass value and run c program in php( web application) 如何在php(Web应用程序)中传递值并运行c程序

Passing data between PHP and C executable in linux 在Linux中传递PHP和C可执行文件之间的数据

I've started with mongoose . 我开始使用猫鼬 Mongoose is a very lightweight webserver. Mongoose是一个非常轻量级的网络服务器。 My current idea is that my web application would send data via POST directly to my c program. 我目前的想法是我的Web应用程序将通过POST直接发送数据到我的c程序。 The request is handle by the mongoose webserver. 请求由mongoose webserver处理。 Is this the way to go? 这是要走的路吗? Or would be a better solution to split it in two application 1) a webserver that handle input output - the variable between both program is passed by shared memory 2) my current c program for my operation (input / output via shared memory) 或者是一个更好的解决方案,将它拆分为两个应用程序1)处理输入输出的网络服务器 - 两个程序之间的变量由共享内存传递2)我当前的c程序用于我的操作(通过共享内存输入/输出)

Or is this all the complete wrong way and a) i should stick to my file solution b) there is a even better way? 或者这是完全错误的方式和a)我应该坚持我的文件解决方案b)还有更好的方法吗? I am sorry for all this question but i am pretty new to c and struggle a lot and not feeling really confident with all this. 对于所有这些问题我很抱歉,但我很陌生并且非常挣扎,并且对这一切并不是很有信心。

Edit 编辑
I try to be more specific what my c program does. 我试着更具体地说明我的c程序。 Basically its a simulation. 基本上它是一个模拟。 It is like a stateflow that have around 20-40 inputs that are all integer and most of the time just 0 or 1. The program acts on the base of the inputs and of previous states. 它就像一个状态流,大约有20-40个输入都是整数,大部分时间只是0或1.程序作用于输入和先前状态的基础。 The inputs are reset after one iteration by changing the input file to origin state. 通过将输入文件更改为原始状态,在一次迭代后重置输入。

The output file is updated all the time with the corresponding output values (like var = 1 var2 = 3) 输出文件始终使用相应的输出值更新(如var = 1 var2 = 3)

您可以使用DBMS进行数据共享。

I would develop this C program as SOAP WebService , this way it can be executed once and wait for requests. 我将这个C程序开发为SOAP WebService ,这样它就可以执行一次并等待请求。 (or eventually work in CGI mode and be executed if needed) (或最终在CGI模式下工作并在需要时执行)

Your PHP script (using SOAP library ) could be able to call a single C function from your program and receive it's result. 您的PHP脚本(使用SOAP库 )可以从您的程序中调用单个C函数并接收它的结果。 All data like function arguments, objects etc is exchanged between different languages using XML syntax. 使用XML语法在不同语言之间交换所有数据,如函数参数,对象等。

I did it once to establish communication between PHP, C and Python, it worked fine. 我曾经做过一次建立PHP,C和Python之间的通信,它运行良好。 I used gSOAP . 我用了gSOAP I's officially for C++, I know it's not proper way to do so, but you can mix them since they are compatible. 我正式使用C ++,我知道这不是正确的方法,但你可以将它们混合起来,因为它们兼容。

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

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