1

How do I execute a Python file from PHP?

I have a Python parser in my project that will get video source from another site. I plan to do so by

  1. Writing in text file url (from PHP).
  2. Starting a Python script (from PHP).
  3. Python script will get that url from text file
  4. Finally Python script will write that video source in other text file
  5. PHP will read that file with source and will use that further.

At 2., how do I start the python script?

Vadim Melnikov
  • 100
  • 1
  • 7
  • What do you mean by "make connection with Python and PHP without text files"? – Chris Nov 15 '20 at 18:24
  • @Chris I think connecting from text files from script to script is not very good decision, so maybe there is a library for this or something like that... – Vadim Melnikov Nov 15 '20 at 18:29
  • 1
    [symfony/process](https://symfony.com/doc/current/components/process.html) is a nice library in addition to `proc_open` or `shell_exec` as @bendem suggested – apokryfos Nov 15 '20 at 18:43
  • @voters at worse this question is a duplicate of https://stackoverflow.com/questions/19735250/running-a-python-script-from-php but it is not unclear, no. – Félix Adriyel Gagnon-Grenier Nov 15 '20 at 22:59

1 Answers1

1

You can take a look at proc_open and shell_exec.

proc_open will allow you to run other code while waiting for the command to finish.

shell_exec will run the command and return the output after the command has finished.

As always, the php documentation has nice examples, I trust you'll find what you seek for.

bendem
  • 83
  • 3
  • 8