我知道StackOverflow上有类似的问题,但我已经尝试了所有这些问题,但没有一个能够运行。在我的笔记本电脑上,我有一个Apache服务器,一个用PHP构建的网站和一个Python脚本。
尝试:
1)system
$mystring = system('python myscript.py myargs', $retval);
2)和3)JSON和$ temp = exec($ command,$ output);
PHP:
#first case
command= 'C:\wamp\www\com\non.py file';
$temp = exec($command, $output);
echo(" output ");
echo $output;
echo " hello ";
echo $temp;
#second case
// Execute the python script with the JSON data
$result = shell_exec('python /confronto/non.py ');
#. escapeshellarg(json_encode($data)));
// Decode the result
$resultData = json_decode($result, true);
// This will contain: array('status' => 'Yes!')
var_dump($resultData);
蟒:
import sys, json
def tests():
b = 2
print("inside test")
print(b)
a = 4
return a
#if __name__ == "__main__":
c = tests()
print("main")
print(c)
# Generate some data to send to PHP
result = {'status': 'Yes!'}
# Send it to stdout (to PHP)
print json.dumps(result)
4)设置了apache配置:
AddHandler cgi-script .cgi .py
答案 0 :(得分:1)
exec()对我来说效果很好。
murftown$ php -a
Interactive shell
php > $output = exec('python myscript.py', $output);
php > echo $output;
{"status": "Yes!"}
php > print_r(json_decode($output));
stdClass Object
(
[status] => Yes!
)
那是在交互式控制台中,这里只是纯php:
$output = exec('python myscript.py', $output);
print_r(json_decode($output));