1

I have Python script with a fucntion. I want to call this function from PHP while passing two parameters to the function.
What am i doing wrong here? :/

myscript.py

def InitPython(ID,Name):
      'DO STUFF'

PHP

 $varone='bleh';
    $vartwo='blah';
    $output = shell_exec("myscript.py '$varone' 'vartwo' ");
2

1 Answer 1

1

PHP:

It could be possible that your myscript.py is not found, use a Full Path:

 $output = shell_exec("<your Full Path>myscript.py '$varone' 'vartwo' ");

myscript.py

You must Read the passed Parameters in your Python Script, for instance:

def InitPython(ID,Name):
  'DO STUFF'

if __name__ == '__main__':
   import sys
   InitPython(sys.argv[1], sys.argv[2])

Relevant: Python Documentation: module-argparse

The argparse module makes it easy to write user-friendly command-line interfaces. The program defines what arguments it requires, and argparse will figure out how to parse those out of sys.argv

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Works Perfectly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.