How to pass php functional parameters from python script and get output?

Question:

I’m working on a python plugin & it needs to retrive some data from php script,
As I tried below I can retrive php output as shell command,

But I need to call start() function from my python script by passing values as start(10,15)

Is there a possible way to call start() php function with parameters from python script ?

PHP Script:

<?php

function start($height, $width) {
    
    return $height*$width;
}

echo start(10,15);  // #1

?>

Python Script:

import subprocess

result = subprocess.run(
    ['php', './check1.php'],    # program and arguments
    stdout=subprocess.PIPE,  # capture stdout
    check=True               # raise exception if program fails
)
print(result.stdout)         # result.stdout contains a byte-string
Asked By: user20907770

||

Answers:

You can pass command-line arguments to a PHP script. See https://www.php.net/manual/en/reserved.variables.argv.php

PHP:

...
echo start($argv[1], $argv[2]);

Python:

result = subprocess.run(
    ['php', './check1.php', '10', '15'],
    stdout = subprocess.PIPE,
    check=True
)
Answered By: Tim Roberts
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.