-1

I've recently been working on a web crawler with PHP and as a newish PHP coder, I'm not the most advanced. I also know a decent bit of Python and there are a few things I am able to do in Python yet not in PHP.

Is there any way for me to run a Python function with parameters inside a PHP script? Please be specific in your answers as I'm not amazing at PHP.

1 Answer 1

2

Run the Python script using php's shell_exec function

Think of shell_exec as basically running commands in your terminal. So if you normally run your python script from the shell like this:

/python-scripts/myscript.py "here is an argument"

You would pass the same command into shell_exec, like this:

$output = shell_exec("python-scripts/myscript.py 'here is an argument'");

Note: shell_exec will return the output of the command you run, so save it to a variable if you want to use the output from the python program in your PHP script.

Edits: fixed syntax issues, provided a more specific example

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

9 Comments

Could you be a little more specific?
I saw that and I am still unsure. Do you first have to import the program and also how do you use parameters in the function? DO you just call them as normal?
see how python arguments are passed in a program.
When I try to run my script from terminal it says No Such File or Directory Found, even though I checked and I used the correct command and its in the correct directory.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.