PHP调用content_verification.py无返回结果求助
问题描述
我尝试实现PHP调用Python脚本处理用户输入的功能,其中email_verification.py可正常返回结果,但content_verification.py通过PHP调用时无法返回任何内容。已添加超时设置但无效,该Python脚本在终端直接运行正常,仅PHP调用时出现问题。
相关代码
content_verification.py
import language_tool_python import re import sys def check_grammar(email_content): tool = language_tool_python.LanguageTool('en-US') matches = tool.check(email_content) return matches def find_triggered_phrase(email_content): SUSPICIOUS_PHRASES = ['urgent', 'bank account', 'password', 'click here', 'login now'] email_content = email_content.lower() for phrase in SUSPICIOUS_PHRASES: if phrase in email_content: return phrase return None def is_suspicious_formats(email_content): SUSPICIOUS_FORMATS = [ r'<a href="http', # 超链接 r'<!DOCTYPE html>', # HTML文档声明 r'<html>', # HTML标签 r'<body>', # HTML标签 r'<script>', # JavaScript标签 ] for format in SUSPICIOUS_FORMATS: if re.search(format, email_content, re.IGNORECASE): return True return False def main(email_content): grammar_errors = check_grammar(email_content) suspicious_phrases = find_triggered_phrase(email_content) suspicious_formats = is_suspicious_formats(email_content) result = "" if grammar_errors: result += "Grammar errors:\n" for error in grammar_errors: result += f"Message: {error.message}\n" result += f"Suggested corrections: {error.replacements}\n" if suspicious_phrases: result += "The input contains suspicious phrases:\n" for phrase in suspicious_phrases: print(f"- {phrase}\n") if suspicious_formats: result += "The input contains suspicious email formats.\n" if not grammar_errors and not suspicious_phrases and not suspicious_formats: result += "No issues found.\n" return result if len(sys.argv) != 2: print("Usage: python content_verification.py <email>") else: email_content = sys.argv[1] result = verify_email(email_content) print(result)
PHP文件
<?php try { if ($_SERVER["REQUEST_METHOD"] == "POST") { $content = $_POST["content"]; echo "Content: " . $content . "<br>"; // 执行内容验证的Python脚本 $content_command = 'timeout 2m python content_verification.py ' . escapeshellarg($content); $content_output = shell_exec($content_command); // 去除输出首尾空白 $content_message = trim($content_output); } } catch (Exception $e) { // 异常处理 $message = 'Caught exception: ' . $e->getMessage(); } ?>
问题排查与解决方法
修复Python脚本函数调用错误
脚本末尾调用了不存在的verify_email函数,这是核心笔误,应改为调用定义好的main函数:result = main(email_content)这个错误会导致脚本执行时抛出
NameError,终端运行时如果用的是修正后的版本能正常工作,但贴出的代码存在此问题,是PHP调用无输出的主要原因。修正可疑短语处理逻辑
find_triggered_phrase返回单个匹配短语(字符串),原代码中for phrase in suspicious_phrases会将字符串按字符迭代,且用print直接输出而非追加到result变量,导致输出混乱。修改为:if suspicious_phrases: result += "The input contains suspicious phrases:\n" result += f"- {suspicious_phrases}\n"PHP端捕获错误输出辅助排查
shell_exec仅捕获标准输出,无法获取脚本错误信息,改用exec同时获取输出和返回码:$content_command = 'timeout 2m python content_verification.py ' . escapeshellarg($content); exec($content_command, $output, $return_var); $content_message = implode("\n", $output); echo "Verification Result: " . $content_message . "<br>"; echo "Script Return Code: " . $return_var . "<br>";返回码非0说明脚本执行出错,可据此进一步排查。
确保Python环境一致性
PHP运行用户(通常为www-data)的Python环境可能与终端用户不同,导致language_tool_python未安装。指定完整Python路径并安装依赖:sudo -u www-data pip install language_tool_python
内容的提问来源于stack exchange,提问作者drag pollet
相关产品推荐
相关产品推荐

