您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在PHP中执行Python脚本并在两者之间交换数据

在PHP中执行Python脚本并在两者之间交换数据

通常,您可以通过使用通用语言格式并使用stdinstdout来传递数据来在语言之间进行通信。

PHP

// This is the data you want to pass to Python
$data = array('as', 'df', 'gh');

// Execute the python script with the JSON data
$result = shell_exec('python /path/to/myScript.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

# Load the data that PHP sent us
try:
    data = json.loads(sys.argv[1])
except:
    print "ERROR"
    sys.exit(1)

# Generate some data to send to PHP
result = {'status': 'Yes!'}

# Send it to stdout (to PHP)
print json.dumps(result)
php 2022/1/1 18:14:54 有616人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶