要在 PHP 中使用的 Python 代码片段 [英] Python code snippet to be used in PHP
问题描述
我是一名 Microsoft 开发人员,然后我会用这些话向大家解释我想做什么以更好地理解问题.
I am a Microsoft developer then I explain what I want to do in those words to everybody to understand the question better.
我有一段 Python 代码,我需要用它来制作一个小网站,并具有让程序接受一个文本并在另一个文本中返回结果的界面.
I have a Python piece of code which I need to make a little website and have the interface for the program to accept one text and return the result in another.
因此,如果我想在 Visual Studio 中执行此操作,我将在 Xaml
中创建我想要的界面,并使用绑定在 C#
中编写核心.然而,我有一个现成的 Python
代码片段,而不是 C# 代码,我不想将它改写为 C
代码.我如何使用 Python
和说 PHP
或任何其他方法来做同样的事情.
So if I wanted to do this in Visual Studio, I would create the interface I wanted in Xaml
and using binding I would write the core in C#
. However instead of C# code I have a ready Python
snippet and I don't want to rework it to the C
code. How can I do the same using Python
and per say PHP
or any other.
PS,我在我的 Linux 机器上准备好了 PHP,并且没有涉及数据库.非常简单的片段.
PS, I have the PHP ready in my Linux machine and there is no database involved in this. quite simple snippet.
将不胜感激详细的答案,但是您不一定需要,如果您向我展示正确的方向,我会挖掘它.
Detailed answers will be appreciated, however you don't need to necessarily and if you show me the right direction I'll dig it up.
推荐答案
解决方案是使用 exec()
因此,如果您使用
import sys
myPythonInputVar = sys.argv[1]
然后您可以将其作为输入参数提供给您要在 PHP
then you can feed it as in input argument to the line that you want to execute in the syntax form below in PHP
<html>
<body>
Your Python code returns:
<?php echo exec("/usr/bin/python /home/myUsername/test.py $myInput");?>
</body>
</html>
这篇关于要在 PHP 中使用的 Python 代码片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!