Pustaka Taratsa

Baca teks (EPUB): PHP: Memanggil python

1 bagian

Teks ini diambil langsung dari berkas EPUB; tata letak dan gambar tidak disertakan. Untuk tampilan lengkap, silakan merujuk pada dokumen asli.

Daftar isi
  1. PHP: Memanggil python
  2. Contoh 1
  3. Contoh 2
  4. Referensi

PHP: Memanggil python

Sumber: https://onnocenter.or.id/wiki/index.php/PHP:_Memanggil_python

Sumber: http://stackoverflow.com/questions/166944/calling-python-in-php

Contoh 1

You can run a python script via php, and outputs on browser.

Basically you have to call the python script this way:

$command = "python /path/to/python_script.py 2>&1";
$pid = popen( $command,"r");
while( !feof( $pid ) )
{
 echo fread($pid, 256);
 flush();
 ob_flush();
 usleep(100000);
}
pclose($pid);

Note: if you run any time.sleep() in you python code, it will not outputs the results on browser.

For full codes working, visit How to execute python script from php and show output on browser

Contoh 2

The backquote operator will also allow you to run python scripts using similar syntax to above

In a python file called python.py:

hello = "hello"
world = "world"
print hello + " " + world

In a php file called python.php:

$python = `python python.py`;
echo $python;

Referensi