有没有办法将python变量返回到php?
因此,我正在制作一个搜索引擎来搜索互联网,该引擎使用python,搜索结果的网页使用php。我需要获取变量(即有没有办法将python变量返回到php?,python,php,search-engine,Python,Php,Search Engine,因此,我正在制作一个搜索引擎来搜索互联网,该引擎使用python,搜索结果的网页使用php。我需要获取变量(即filelistlist)并将它们返回到php脚本。请原谅任何打字错误 <html> <head> <script src='searchengine.js' type='text/javascript'></script> </head> <body> <
filelist
list)并将它们返回到php脚本。请原谅任何打字错误
<html>
<head>
<script src='searchengine.js' type='text/javascript'></script>
</head>
<body>
<?php
$cmd='python3 searchresult.py'+$_POST["searchquery"];
$output = shell_exec($cmd);
echo $output;
?>
</body>
</html>
是python代码。您可以将
文件写入json文件,然后用php读取json文件
蟒蛇
PHP
shell\u exec应该从python脚本中捕获标准输出。尝试在脚本末尾添加一个print语句:print(filelist)
在PHP中,您使用
而不是+
连接字符串,因此它可能应该是$cmd='python3 searchresult.py'$_POST[“搜索查询”]代码>。但是,将用户数据直接执行到shell_exec()中是危险的。始终首先转义数据:。您也没有将数据作为参数传递给python文件,而是将搜索添加到python文件的文件名中。您需要在.py
之后留一个空格。对于Mike67:我不需要显示输出,我需要将值返回到php脚本端注意:jQuery只是JS中的一个库构建,因此如果您使用jQuery,您使用的是JS,因此“or”在这里有点误导。同样,你不应该仅仅给出一个抽象的答案,你应该展示一些真实的例子来说明你的意思。@Magnus Eriksson,这更好吗?
import sys
from os import listdir, basename
import re
keyword=sys.argv[1]
keyword=str(keyword)
filelist=[]
db='C:\\Users\\monik\\Documents\\searchengine\\db'
filestoparse=listdir(db)
for file in filestoparse:
with open(file, 'r') as f:
filecontent=str(f.read)
result=re.findall(keyword, filecontent)
if len(result)>=5:
filelist.append(basename(f.name))
import json
#parse files in directory to dictionary
filestoparsetodict ={'file1':'myfile.cdr', 'file2':'picture.psd'}
# write your dictionary to json file
out_file = open("C:/xampp/htdocs/phpjson/myfile.json", "w")
json.dump(filestoparsetodict, out_file)
out_file.close()
<?php
$filelist = file_get_contents("myfile.json");
// Convert content to array
$myarray = json_decode($filelist, true);
echo $myarray["file1"]; // print an item from array objects
?>