简体   繁体   English

使用参数从PHP调用python

[英]calling python from PHP with arguments

I have a python script on local machine sending data to PHP script on google cloud compute engine. 我在本地计算机上有一个python脚本,将数据发送到Google云计算引擎上的PHP脚本。 I want my PHP script to send that data to another python script as arguments for processing. 我希望我的PHP脚本将数据发送到另一个python脚本作为处理参数。 I am trying this, php script 我正在尝试这个PHP脚本

<?php
$time = $_GET['time'];
exec("python3 caller.py ".$time,$output);
ar_dump($output);
echo $output;
?>

Did you try to look up shell_exec http://php.net/manual/en/function.shell-exec.php and PHP shell_exec() vs exec() ? 您是否尝试查找shell_exec http://php.net/manual/zh/function.shell-exec.phpPHP shell_exec()与exec()

In addition i cant see your dir structure but php will probably require you to declare where from you execute your scripts. 另外,我看不到您的目录结构,但是php可能会要求您声明从何处执行脚本。 http://php.net/manual/en/function.dir.php http://php.net/manual/en/function.dir.php

You need to pass variables like below 您需要像下面那样传递变量

$prog = 'pathtopythonfile/filename.py '.$variable1.' '.$variable2;
shell_exec($prog);

inside python file, u can get variables like below 在python文件中,您可以获取如下所示的变量

import sys

var1 = sys.argv[1]
var2 = sys.argv[2]

I cant add comments at the moment so you could see this as a comment: What output do you get or do you get any errors? 我目前无法添加评论,因此您可以将其视为评论:您得到什么输出或出现任何错误? You're asking for help but not saying what's wrong. 您是在寻求帮助,而不是说出什么问题了。

Also there is a system function in php for this case: http://php.net/manual/en/function.system.php 在这种情况下,php中也有一个系统函数: http : //php.net/manual/en/function.system.php

$output = system("python3 caller.py ".$time, $executioncode);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM