1

I want to develop a laravel app that get data from python script, what do i need to configure?

I already search how to connect python output via Symfony, but I can't imagine what file I need to change in my Laravel app from this tutor

https://www.sandervanhooft.com/blog/laravel/how-to-use-laravel-with-python-and-the-command-line/

from the beginning, I just want to get the output from python script like text output "hello world".

2 Answers 2

1

The article you linked says that the code is for Laravel.

use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

// $json = an encoded JSON string
$process = new Process("python3 /Path/To/analyse_json.py {$json}");
$process->run();

// executes after the command finishes
if (!$process->isSuccessful()) {
    throw new ProcessFailedException($process);
}

$output = $process->getOutput();
$jsonDoc = json_decode($output, true);
dump($jsonDoc);

It just uses a Symfony component which probably can be installed separately via Composer, or maybe even already included in the standard Laravel installation (Laravel itself uses some Symfony components too).

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

4 Comments

You should mention that this is a potential security risk if the JSON document is not properly sanitized first.
@exhuma it depends on how you use it. If at some point you somehow execute some part of the received content (not necessarily JSON), output it as HTML, etc., then yeah, it should be sanitized.
where should i add/create that file in my laravel app??@AlexP.
@alvinChristianto You can put it anywhere, it just runs the specified script, puts the output into a variable, parses it (it supposed to be JSON in this example), and then calls the dump function to write it somewhere (to the server response if you are calling it from a request handler, such as a controller, or to console if running from CLI), you can of course do whatever you want with it.
1

finally after a few day of searching, i try to make simple print statement of python script, like

color ='red'
print "my color is " + color

then, in controller(laravel app), i add some php code like : call python with php. I replace echo command with dd($output), then i got the output. thanks all for your answers.

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.