首页 > 解决方案 > 使用 PHP 在 HTML 网页中执行外部 python 脚本

问题描述

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

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

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

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

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

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

标签: javascriptpythonphphtml

解决方案


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 的响应来显示


推荐阅读