0

I need a PHP code that outputs an image generated by matplotlib. The input is given by PHP, while python processes it and returns an image.

For this matter, I'm trying to "call python" via PHP.

I've already tried exec(), popen(), proc_open(), system(), shell_exec() and even backticks (the last two seem to be currently disabled).

I know that I should use proc_open() for my project, but I can't even display a simple "Hello World" with any of these functions.

Even after researching in forums and reading the PHP documentation, the result is always blank.

What should I do?


Here is a sample code that, doesn't output anything for me:

PHP file:

<?php
$pyscript = 'C:\wamp64\www\my_folder\test.py';
$pypath = 'C:\Python36\python.exe';
$cmd='$pyscript $pypath';
exec("$cmd", $output);
echo $output;
?>

Python File:

print("Hello World")
nickh
  • 279
  • 3
  • 12
  • 1
    Dear Obsidian Age and DYZ, I've already tried to replicate this exact same answer, I've already added `#!/usr/bin/env python` and saw dozens of similar posts. Anyway, I couldn't solve the problem. I respectfully ask you to remove the "duplicate" label from my question. – nickh Aug 17 '18 at 03:43
  • You probably left out `$pypath` before the `$pyscript`, and also what is `$python`?, try `$cmd='$pypath $pyscript';` – metatoaster Aug 17 '18 at 03:43
  • If I do `$cmd='$pypath $pyscript';`, it actually gives me the error **Notice: Array to string conversion** when I try to `echo $output;` Probably it isn't running the python code properly, but instead trying to run the code written in the .py file with PHP. – nickh Aug 17 '18 at 03:52
  • 1
    That would be because `$output` is an Array as per [documentation on `exec`](http://php.net/manual/en/function.exec.php); [a reference question](https://stackoverflow.com/questions/30499423/notice-array-to-string-conversion-when-echoing-the-array) about this error message. – metatoaster Aug 17 '18 at 03:57
  • Thank you! Unfortunately, there is still a problem with the output. Even if I write `$output[0]`, I get **Notice: Undefined offset: 0** – nickh Aug 17 '18 at 04:14
  • Using `print_r()` or `var_dump()` gives me an empty array as output. It means that the .py file is not generating any output. – nickh Aug 17 '18 at 04:23
  • Again, please consult the documentation on `exec` and see that there are specific caveats to achieving this on Windows, and that `safe_mode` is also a factor in this. – metatoaster Aug 17 '18 at 05:01

0 Answers0