Home >Backend Development >PHP Tutorial >Run Python program from PHP

Run Python program from PHP

WBOY
WBOYforward
2023-09-05 20:25:031674browse

Run Python program from PHP

In PHP, you can use the ‘shell_exec’ function. It can be executed via the shell and returns the result as a string. If NULL is passed from the command line or there is no output, an error is returned.

The following is a code example with the same function:

<?php
   $command_exec = escapeshellcmd(&#39;path-to-.py-file&#39;);
   $str_output = shell_exec($command_exec);
   echo $str_output;
?>

Need to give the correct permissions in order to successfully execute the Python script.

Note − When working on Unix type platforms, the PHP code is executed as the web user. Therefore, web users should be given the necessary permissions on directories and subdirectories.

The above is the detailed content of Run Python program from PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete