我正在建立一个Joomla 3网站,但我需要自定义很多页面。我知道我可以在Joomla中使用PHP,但是也可以在其中使用Python吗?具体来说,我希望使用CherryPy编写一些自定义代码段,但我希望它们显示在 native Joomla页面(而不仅仅是iFrame)中。这可能吗?
最佳答案
PHP执行Python“脚本”
这将对脚本起作用,该脚本负责填充并返回输出,不适用于CherryPy。
<?php
// execute your Python script from PHP
$command = escapeshellcmd('myPythonScript.py');
$output = shell_exec($command);
echo $output;
// take response content to embed it into the page
?>
PHP访问Python/CherryPy服务的网站
import cherrypy
class HelloWorld(object):
def index(self):
return "Hello World!"
index.exposed = True
cherrypy.quickstart(HelloWorld())
这将启动
http://localhost:8080
,您应该会看到Hello world!
。现在,您可以通过访问它的
localhost:port
来访问CherryPy的输出。性能不好,但可以。
<?php
$output = file_get_contents('http://localhost:8080/');
echo $output;
?>
Joomla + Ajax访问Pyhton/CherryPy服务的网站
一种替代解决方案是不使用PHP来获取内容,而是从客户端进行获取。基本上,您将对CherryPy服务的网站使用Ajax-Request来获取其内容,并将其嵌入到Joomla服务的页面的dom中。
// add jQuery Ajax reqeust from your Joomla page to CherryPy
$.ajax({
url: "https://localhost:8080/", // <-- access the 2nd served website
type: 'GET',
success: function(res) {
//console.log(res);
alert(res);
$("#someElement").html(res);
}
});