Amr
Amr

Reputation: 59

How to run python from php laravel?

I am trying to call python script from php, everything works perfect in plain php like this:

    $data = $_POST['text'];
    $result=exec('python_file.py ' . escapeshellarg(json_encode($data)));
    $resultData = json_decode($result, true);

The problem is, when I use the same line in laravel 3.2.13 function in one of my bundles controllers, it doesn't execute the command and gives no error!

Is there any help?

This is how my laravel function looks like :

public function action_test(){
    $text="Test";
    $result=exec('python_file.py ' . escapeshellarg(json_encode($text)));
    $resultData = json_decode($result, true);
    dd($result);

}

Thank You

Upvotes: 0

Views: 2852

Answers (2)

Amr
Amr

Reputation: 59

Problem is solved now : Solution : in my python code , it was trying to call other files relatively like this :

configFileDatasetBuilder = ".\\Configurations\\Config.xml"

but as I understand , I'm calling the whole thing from a controller function which has a request path different from file paths , so it loses it's path . As for now I hardcoded the python paths like :

 configFileDatasetBuilder =" MyDrive:\\xampp\\htdocs\\MyProject\\bundles\\myBundle\\controllers.\\Configurations\\Config.xml"

and it worked : ) Thanks every one

Upvotes: 0

Nikhil Agrawal
Nikhil Agrawal

Reputation: 285

  1. Please make sure u have enabled the debug mode in config file and check if there is any error log.

  2. Check if the file path is correct or not ? and also the file permissions

3 . As per PHP doc, "If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function."

Hope it will help, if it don't then please comment below so that we can solve it further.

Thanks

Upvotes: 1

Related Questions