gpt4 book ai didi

php - 无法在 php 中读取 python 的输出

转载 作者:行者123 更新时间:2023-11-29 03:22:28 25 4
gpt4 key购买 nike

我无法从 python 脚本获取输出数据到 php。 php中没有抛出错误,python脚本从命令行完美输出。 python :

import json,sys,MySQLdb
db = MySQLdb.connect(host="localhost", user="root", passwd="root", db="sr")
cur = db.cursor()
def requestQuiz():
sql = "SELECT quiz FROM wordbank ORDER BY RAND() LIMIT 1"
cur.execute(sql)
result_set = cur.fetchall()
quiz = result_set[0][0]
return json.dumps(quiz)

if __name__ == "__main__":
#print solve(sys.argv[1])
print requestQuiz()

PHP

<?php
$result = exec("python request.py");
$results_array = json_decode($result);
echo "<p>".$results_array."</p>";
?>

python 应返回引号中的单个单词,例如 "happy"当我从命令行尝试它时它会这样做

python request.py

我已经确认 php 和 python 在同一目录中并且 python 脚本名为“request.py”

最佳答案

尝试使用 shell_exec()。就像这样..

<?php
$result = shell_exec("python request.py");
$results_array = json_decode($result);
echo "<p>".$results_array."</p>";
?>

更多 http://php.net/manual/en/function.shell-exec.php

关于php - 无法在 php 中读取 python 的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41885810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com