This question already has answers here:
php : Capturing the command output
(3 answers)
Closed 8 years ago.
I have a PHP program, and a Python program. You can think that the Python program is like a robot or machine. Typically, it will use a database, do some analysis and return result for you. It is a plain text console application.
I would like to let the user use the program via a web interface. So, I would like to use PHP, accept user input, and the pass it to the Python program, and back to the user.
How can I achieve it? The Python program is always running on the server, which will keep analysis data from the web and the user input, so it can't easily convert to become a script on server side.
Any ideas?
You might want to check exec function. You can pass the input from user as parameters to the python script and get back the output from it.
Take a look into http://www.csh.rit.edu/~jon/projects/pip/
Related
This question already has answers here:
Execute php code in Python
(5 answers)
Closed 5 years ago.
Would like to know whats the Python function to call if I would like to write PHP script in Python.
I am trying to utilise a PHP function that would take in values from a table created in python, operate on the text in PHP (using that PHP library) and throw the result back into a python table.
The question here: Execute php code in Python has some answers which may help you. Notably this one by Sjoerd:
import subprocess
result = subprocess.run(
['php', 'image.php'], # program and arguments
stdout=subprocess.PIPE, # capture stdout
check=True # raise exception if program fails
)
print(result.stdout) # result.stdout contains a byte-string
It should be reasonably easy to make that work for your use case. In this case rather than directly writing PHP in python you will write the php in another file and then call that with python and capture its results.
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
I have a page with a java script that adds a row to a table. On the same page there is a PHP script that should read the table and add it to the database. How do I read the table using PHP ?
What you're thinking of will not directly work.
A Javascript script is executed on the client side only, whereas a PHP is executed on the server side only, and before the datas are sent to the client.
PHP can not directly read the table.
What you can do is use AJAX to send the data from the user's browser to a PHP script that'll use them.
You'll find more infos here.
PHP is serverside programing, the PHP code serves data to the client - PHP can't read from the client.
If you add a row using javascript, PHP has no way of getting that info.
You'll need to use Ajax to solve your specific problem.
You cannot "read the table" with PHP. You will have to send the data of the table to the server (and read it with PHP) using JavaScript or CGI.
A well-known technique to do this is called Ajax.
This question already has answers here:
Why doesn't exec("top"); work on Linux?
(5 answers)
Closed 9 years ago.
Is it possible to display the commands like 'top' on webpage using php?
<?php
echo shell_exec('top');
?>
maybe this:
<?php
$output = null;
exec('top -n 1', $output);
var_dump($output);
?>
If I understand your question correctly, you're trying to get an interactive program showing on the client with live updates. This is not possible as you've demonstrated.
It seems you may not understand what's going on with PHP. PHP runs on the server before the page is downloaded by the client. The client then gets a 'snapshot' of the page as the server rendered it. Once the page is loaded on the user's machine, the server cannot touch the page.
To get interactive content, you have a few options (from least desirable and easiest to most desirable and most involved):
refresh the page
make periodic requests for updates (AJAX)
have the server push down changes (COMET, WebSockets)
Another problem is that interactive commands like top use a bunch of terminal-specific (refresh the terminal, rewrite bits of text, etc.) that will mess up the output in the browser. You'll need to do something like #David said and get a snapshot of the output and get that to the user periodically (choose one from above).
There are lots of libraries and tutorials for PHP available for whichever route you choose.
This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 9 years ago.
I have a code and need help. I have a curl call with a bash sign like this
http://example.com.ua/script.php
clients send me data using a script but some send me the bash # which I need to save all the data to my database. When I get $_SERVER I don't see all the data how can get all the data?
for example:
I have
http://example.com.ua/script.php#code1234
I need to save:
example.com.ua/script.php#code1234
but I see
example.com.ua/script.php
PHP does not have access to the location hash, period.
You only have access to querystrings in PHP, and if you need to get the hash, you would have to use javascript to get it, and send it to your PHP script with ajax.
I want to implement one logic which is written in python, this code will do some searching stuffs, and I have a website done in PHP. can any one tell me whether I can include python script in PHP? if yes , how can I do that ?
Criteria :
Input to the python script will come from php or html [either text or file]. and output of python is directly displayed to the page or through php or store it in mysql and show it through PHP.[Please suggest me the best one in this].
If you have access to exec, you can run the python interpreter. However, that's:
Overkill
Not necessarily wise
A major waste of resources
If your logic is simple, why don't you write it in PHP? Furthermore, if your logic is not simple...why don't you make an API of some sort to access it and favour communication rather than code deduplication?