在我的laravel-API中,我必须调用main.py文件并将波斯字符串UTF-8传递给main.py
这是laravel-API
$command = 'سلام چطوری؟'
$output = '';
exec("python ../../../python/main.py '$command'", $output);
dd($output);这是main.py
import sys
def main(text):
return text
x = sys.argv[1]
print(x)问题是,当我传递这样的东西时,它正确地工作。
$command =“嗨,你好吗?”
//output
['hi how are you?']但是当我像这样传递波斯线时,我没有得到任何回报。
$command =‘سلامچطوری؟’
#output
[]发布于 2021-09-11 15:16:02
将此代码放在调用exec()或shell_exec()之前
setlocale(LC_CTYPE, "en_US.UTF-8");
putenv("PYTHONIOENCODING=utf-8");当您想将$command作为参数传递时,使用utf8_encode(),对于输出使用utf8_decode()。
我已经用shell_exec()测试过这个,它对我来说很好。
摘要:
setlocale(LC_CTYPE, "en_US.UTF-8");
putenv("PYTHONIOENCODING=utf-8");
$command = utf8_encode($command);
$output = shell_exec("python path/to/your/.py/file '$command'");
$output = utf8_decode($output); // your final outputhttps://stackoverflow.com/questions/69098912
复制相似问题