I'm trying to send data from the client to the server. The client runs a simple python script that uses the 'request' library. The server side consists of another simple php script using the $_POST.
I need the webpage to update depending on the data that is given through the client program.
Here is the python script:
import requests
url = "http://xxxxxxx.com/php_files/text_data.php"
d = {'test': 'It works!'}
r = requests.post(url, data = d)
print r.status_code, r.reason
print r.text
And here is the php code:
<!DOCTYPE = html>
<html>
<head>
<h1>
<?php
$txt = $_POST['test'];
echo $txt;
?>
</h1>
</head>
</html>
I need the php page to display 'It works!' on h1 as this is the value that is being passed. But for some reason, it does not display anything
r.text prints the required format with 'It works!' in the < h1 > tags, but the same does not get displayed in the actual website.
I've also tried var_dump($txt). It gives me a NULL value.
Any help would be gladly appreciated.
It seems to me that you are asking a separate instance to update your current instance. The PHP that you are accessing in your browser knows nothing about the python script. It doesn't call the python script at all. In the second session the python script calls the PHP and receives the correct response.
These are two different sessions, the browser window will see nothing from the python script unless it calls it.
Here is what is happening:
Session 1
Session 2
There is no persistence in the first session to save the information for the second session. They are two completely separate actions. A more typical way would be to set up a database (or just quick and dirty a text file) on the server to save the information. You need to create a second PHP file to save the information to a database or text file on the server. You then need to modify your previous PHP file to read the information from the database or the text file. The sessions would then be set up the following way.
Session 1
Session 2
If you really want to use the results from the python script in the PHP without a database or text file, you will need to upload the python script to your server, and use one of the methods suggested in Calling Python in PHP