donglang7236 2018-05-10 01:38
浏览 272

执行Python脚本以返回PHP中的值

I've read a lot of answers on here regarding this but I still haven't been able to figure it out. I have some PHP code that is trying to execute a Python script while the PHP also passes the Python some arguments, and then I want the Python to do something and return a value to the PHP.

I've tried it this way:

$url = "URL given";
$retailer = "other string";

$price = system("/usr/local/bin/python3 /full/path/script.py '" . $url . "' '" . $retailer . "'", $retval);
sleep(5);
print "Price: " . $price;

With the Python code (where the function being called prints the value):

def main():
    getPrice(sys.argv[1], sys.argv[2])
main()

And the $price variable doesn't return anything even though if I run the command thats inside system() in Terminal it prints the value out fine.

I've also tried:

exec("/usr/local/bin/python3 /full/path/script.py", $output, $ret_code);
sleep(5);
print "Price: " . $output[0];

But in this case I've tried it the way above where the Python simply prints "hi" instead of passing the arguments through and I still get nothing, as well as trying it the way with passing the args in from earlier.

I can't tell if maybe the Python isn't being called, or perhaps it is and it returns nothing for some unknown reason but if anyone may know the issue here I would love to hear your thoughts

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法