1

StackOverflow に同様の質問があることは知っていますが、それらすべてを試しましたが、どれも機能しませんでした。私のラップトップには、Apache サーバー、PHP と Python スクリプトで構築された Web サイトがあります。

試み:

1)システム

$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 
4

1 に答える 1

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));
于 2013-07-12T22:36:11.317 に答える