Can I use a php file on a server, to send data to a C# file which then validates the data, and sends the result back to the php file?

huangapple go评论71阅读模式
英文:

Can I use a php file on a server, to send data to a C# file which then validates the data, and sends the result back to the php file?

问题

只返回翻译好的部分:

I'll try to be as simplistic and transparent as I can.
My goal is to: send leaderboard data to a server, have it validated, then added to a MySQL table.

How I want to achieve this:

  1. Player completes game, gets highscore data.
  2. The highscore data is sent to a file on the server, "leaderboard.php".
  3. The leaderboard.php file sends the playthrough data to a C# file on the server, "PlaythroughSimulator.cs" to run a simulation of the playthrough.
  4. After the PlaythroughSimulator.cs file has confirmed or denied the validity of the playthrough, it sends a true/false value back to the leaderboard.php file.
  5. If the recording was valid, the leaderboard.php file adds it to the table as expected.

I have the php process working for receiving and adding scores to the leaderboards on the server, and I've created the playthrough simulator in C# OUTSIDE of the server.
My problem now is bringing the playthrough simulator onto the server and making it work entwined with the php script.

I did a bit of reading and see that I should be able to call C# scripts from php, but I'm not sure how to send or retrieve data between them.

英文:

I'll try to be as simplistic and transparent as I can.
My goal is to: send leaderboard data to a server, have it validated, then added to a MySQL table..

How I want to achieve this:

  1. Player completes game, gets highscore data.
  2. The highscore data is sent to a file on the server, "leaderboard.php"
  3. The leaderboard.php file sends the playthrough data to a C# file on the server, "PlaythroughSimulator.cs" to run a simulation of the playthrough
  4. After the PlaythroughSimulator.cs file has confirmed or denied the validity of the playthrough, it sends a true/false value back to the leaderboard.php file
  5. If the recording was valid, the leaderboard.php file adds it to the table as expected.

I have the php process working for receiving and adding scores to the leaderboards on the server, and I've created the playthrough simulator in C# OUTSIDE of the server.
My problem now is bringing the playthrough simulator onto the server and making it work entwined with the php script.

I did a bit of reading and see that I should be able to call C# scripts from php, but I'm not sure how to send or retrieve data between them.

答案1

得分: 2

通常情况下,计算机上的不同程序不知道或不关心彼此是用什么语言编写的;它们关心它们所使用的协议 - 它们期望的输入、它们将提供的输出以及需要传递信息的位置。

例如,当您在浏览器中有数据并希望将其发送到一个PHP程序时,浏览器不需要知道PHP是什么;相反,它使用HTTP协议将数据发送到互联网上。在服务器上,有一个进程监听该请求,然后 恰好 您设置了它以运行您的PHP脚本。

同样,您不会告诉PHP“运行一个C#文件”;您将告诉它将一些数据发送给另一个程序,而那个程序恰好是用C#编写的。这的一个重要结果是,它有助于您 分解问题,这在编程中始终是一个关键技能,因为您可以直接运行您用C#编写的程序,确认它的行为,然后再告诉PHP与其通信。

在这种情况下使用的最简单的“协议”是一个命令行脚本:

  1. 编写一个C#程序,将游戏数据作为命令行参数传递,或从文件中读取它,并输出一个结果。
  2. 在您的PHP代码中使用 shell_exec 运行该脚本,将数据传递给它,或传递包含数据的临时文件的名称。
英文:

Generally speaking, different programs on a computer don't know or care what language each other are written in; they care what protocols they speak - what inputs they expect, what outputs they'll give, and where that information needs to be passed.

For instance, when you have data in the browser and you want to send it to a PHP program, the browser doesn't need to know what PHP is; instead, it sends the data over the internet, using the HTTP protocol. On the server, a process listens for that request, and it just happens that you've set that up so that it runs your PHP script.

In the same way, you won't be telling PHP to "run a C# file"; you'll be telling it to send some data to another program, and that program will happen to be written in C#. An important consequence of this is that it helps you break the problem down, always a key skill in programming, because you can run the program you wrote in C# directly, and confirm how it behaves, before telling PHP to talk to it.

The simplest "protocol" to use in this scenario is a command-line script:

  1. Write a C# program that takes the playthrough data as a command-line argument, or reads it from a file, and outputs a result
  2. Use shell_exec in your PHP code to run the script, passing it the data, or the name of a temporary file containing the data

huangapple
  • 本文由 发表于 2023年2月18日 20:42:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493418.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定