Running python script in Laravel

前端 未结 3 689
再見小時候
再見小時候 2020-12-01 03:41

So, I am trying to run a python script in my Laravel 5.3.

This function is inside my Controller. This simply passes data to my python script

public          


        
3条回答
  •  失恋的感觉
    2020-12-01 04:46

    Would be handy to use Symfony Process. https://symfony.com/doc/current/components/process.html

    Make sure symphony is available to use in your project

    composer show symphony/process
    

    If not installed do composer require symfony/process

    And then do some thing like

    use Symfony\Component\Process\Process;
    use Symfony\Component\Process\Exception\ProcessFailedException;
    
    //$process = new Process('python /path/to/your_script.py'); //This won't be handy when going to pass argument
    $process = new Process(['python','/path/to/your_script.py',$arg(optional)]);
    $process->run();
    
    // executes after the command finishes
    if (!$process->isSuccessful()) {
        throw new ProcessFailedException($process);
    }
    
    echo $process->getOutput();
    

提交回复
热议问题