Print Python output by PHP Code Print Python output by PHP Code php php

Print Python output by PHP Code


If i understand it well, your python scraper output to a file and you want to "live" display the output via php. What about doing and loop in php in which you use filemtime to know whether or not the file has been updated? You might add a little sleep in order not to overload your server.

If your are using a web page, you may use AJAX to reload only the concerned part of the page at a regular interval.

Hoping this helps you.


Simple case

Assuming execution of scraper is limited to php runtime, run it via popen: http://php.net/manual/en/function.popen.php

More involved case

If you want scraper to run on background and only connect to it vis php from time to time, you can either use some pub/sub toolkit or implement a small web server in the scraper that you can fetch result updates with fopen("https://localhost:port/...") or curl. Many other rpc mechanisms are possible, domain sockets, watching a file, sysv rpc...


I'd communicate using stdout instead of a file. Meaning the python script writes to stdout and the php script reads that.

Using proc_open you can control the python process from php and also read it's output.