javascript - 使用 PHP 在 HTML 网页中执行外部 python 脚本
问题描述
我是这个领域的新手,也是我的生物学背景。
我正在研究生物数据库。我已经使用 MySQL、HTML、PHP 和 Bootstrap 开发了一个数据库,但是,我想添加一个分析模块,例如用 python 编写的算法。
例如,我想添加一个序列搜索算法。(例如,我的数据库中已经有序列,当有人输入他/她的序列时,我的数据库将显示相似的序列)
但是,我找不到任何合适的教程来说明如何运行外部 python 脚本并在网页上显示结果。
对于 PHP,我了解了 shell_exec() 函数,但是当我运行嵌入其中的脚本时,结果什么也没有显示。
任何人都可以帮助我以一种简单的方式将我的 python 脚本与 PHP 连接并在 HTML 网页上显示结果。
解决方案
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 的响应来显示
推荐阅读
- javascript - 如何禁用 Highcharts 导出选项中的某些选项?
- java - 如何在 Apache FOP 2.3 中将字体呈现为形状
- jakarta-ee - 如何从 Wildfly (v17) 服务器添加和读取列表
- java - 如何根据变量选择同一张表的不同数据
- data-structures - 为字母方程创建二叉树
- php - 如何上传图片并将网址保存在数据库中?
- c - 将 Arduino 连接到 TCP 并执行 HTTP Post
- ios - 如何在这样的 iOS 应用程序中创建图表?
- python - 为什么 whoosh Search 在 sklearn 中的性能比 tfidfvectorizer 差?
- java - 引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:运行测试时