2013-06-26 66 views
1

我知道在StackOverflow上有類似的問題,但我已經嘗試了所有這些方法,但都沒有工作。在我的筆記本電腦上,我有一臺Apache服務器,一個使用PHP和Python腳本構建的網站。我如何在php中嵌入python腳本?

嘗試:

1)system

$mystring = system('python myscript.py myargs', $retval); 

2)和3)和JSON $溫度= EXEC($命令,$輸出);

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

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));