-2

I'm using Laravel framework and just experimenting something. Is there a way to use a function from a python script and use it in a PHP script?

By the way, my python script contains multiple functions. And the function I need requires parameters. Do I need to isolate the needed function in another script?

Please help this beginner! Thank you in Advance.

5
  • 1
    as an alternative, you could create an api for your python and for the php just simply make an http request to that api made from python, send it using post or get or json or whatever fits with your requirement Commented Jan 21, 2020 at 1:39
  • use API or create a python scripts then run it on the terminal using artisan or exec function Commented Jan 21, 2020 at 1:39
  • 1
    Have you done any research? Commented Jan 21, 2020 at 1:43
  • 1
    stackoverflow.com/questions/6094643/… Commented Jan 21, 2020 at 2:04
  • please edit your question to clarify your specific problem or add additional details to highlight exactly what you need. Commented Jan 21, 2020 at 3:18

1 Answer 1

1

Create a python script to call your desired function. (Reference)

Look into Symfony's Process class. (Reference)

Use the Process class to run your python script:

use Symfony\Component\Process\Process;

$process = new Process(['python', 'your_script.py', 'argument1', 'argument2']);
$process->run();

if ($process->isSuccessful()) {
    $output = $process->getOutput();
    // do something with $output
} else {
    // handle failure
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.