0

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

3
  • 1
    $result will contain only the last line of the output. If you want the complete output you need to pass a second parameter, for example: exec('python_file.py ' . escapeshellarg(json_encode($text)), $result); Commented Sep 6, 2014 at 11:18
  • ok , I did , now there's a problem in paths , I call the file full path like this : MyDrive:\xampp\htdocs\MyProject\bundles\myBundle\controllers\python_file.py it gives this error: [link] FileNotFoundError: [Errno 2] No such file or directory: '.\\config.xml' Commented Sep 6, 2014 at 11:56
  • can you do me a solid & use shell_exec instead of exec. Let us know if you get the full string output. Commented Jan 11, 2017 at 12:15

2 Answers 2

1
  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

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.