在我的laravel-API
中,我必须调用main.py
文件并传递波斯语stringUTF-8
tomain.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
[]
将此代码放在之前调用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 output