【问题标题】:Executing an external python script in HTML webpage using PHP使用 PHP 在 HTML 网页中执行外部 python 脚本
【发布时间】:2020-05-19 13:46:52
【问题描述】:

我是这个领域的新手,也是我的生物学背景。

我正在研究生物数据库。我已经使用 MySQL、HTML、PHP 和 Bootstrap 开发了一个数据库,但是,我想添加一个分析模块,例如用 python 编写的算法。

例如,我想添加一个序列搜索算法。 (例如,我的数据库中已经有序列,当有人输入他/她的序列时,我的数据库将显示相似的序列)

但是,我找不到任何合适的教程来说明如何运行外部 python 脚本并在网页上显示结果。

对于 PHP,我了解了 shell_exec() 函数,但是当我运行嵌入其中的脚本时,结果什么也没有显示。

任何人都可以帮助我以一种简单的方式将我的 python 脚本与 PHP 连接并在 HTML 网页上显示结果。

【问题讨论】:

  • 您不能从 HTML 页面在服务器上执行任何操作。想想看,我的浏览器没有在你的服务器上运行,希望我无法连接到它,因为这会使你的网站很容易被黑客入侵

标签: javascript python php html


【解决方案1】:
I do not you are using the same server or different server, but you should use the pythong code (any other language) as the api and call it from you html code using ajax and display at your page.
you can do it in your php code using curl. 
you can try curl as follows - 

//  Initiate curl
$ch = curl_init();
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url); //url will be your python code url independent from you php project code

// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);

// Will dump a beauty json :3
var_dump(json_decode($result, true));

you can read the curl from [here][1]

您可以在 html 视图中使用来自 url 的响应来显示

【讨论】:

  • 非常感谢您的详细回复。我会检查它,然后在这里分享给你
猜你喜欢
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
  • 2021-12-06
  • 2012-08-09
  • 2015-08-30
  • 1970-01-01
  • 2018-06-03
  • 1970-01-01
相关资源
最近更新 更多