I have a python script that does pa POST request to my php page (json format). After posting I see from the response that the data is sent at PHP through print(r.text)
in python script.
The problem is that I can't use the data from PHP side - I cannot print it or write to files etc.
What could I be doing wrong here?
Part of python script:
data_json = {
"measurementTime" : first_line.split(',')[0],
"windSpeed" : first_line.split(',')[1],
"windGust" : first_line.split(',')[2],
"windSpeedCount" : first_line.split(',')[3],
"rain" : first_line.split(',')[12],
"windDirection" : first_line.split(',')[13],
"inputVoltage" : first_line.split(',')[14],
"solarRadiation" : first_line.split(',')[15],
"temperature" : first_line.split(',')[16],
"windSpeed" : first_line.split(',')[18],
"humidity" : first_line.split(',')[19],
"barPressure" : first_line.split(',')[20]
}
url = 'http://localhost/index.php'
r = requests.post(url, data=json.dumps(data_json))
print(r.text) # here I can see that data has been sent
print(r.status_code)
PHP (index.php):
<?php
$json = file_get_contents('php://input');
print_r($json);
?>
Python script output:
...
<body>
{"measurementTime": "2018-01-25 17:52:25", "windSpeed": "9.0", "windGust": "0.0", "windSpeedCount": "0", "rain": "1.50", "windDirection": "0", "inputVoltage": "13.51", "solarRadiation": "", "temperature": "0.00", "humidity": "87.5", "barPressure": "1004.473"}
</body>
</html>
The problem is that I can't use the data from PHP side - I cannot print it or write to files etc
Here is a php script that will write the python json to a file:
<?php
file_put_contents(
"../tmp/data.txt",
file_get_contents('php://input') . "\n"
);
echo "Got it";
?>
The only problem I had was with permissions to write the file. I'm using apache2, so I created a tmp
directory under the apache2 directory, and I gave all users the ability to write to the directory:
/usr/local/apache2/$ mkdir tmp
/usr/local/apache2/$ ls -al
...
...
drwxr-xr-x 2 7stud admin 64 Jan 25 13:23 tmp
/usr/local/apache2$ chmod a+w tmp #all+write, where all=user,group,other
/usr/local/apache2$ ls -al
...
...
drwxrwxrwx 2 7stud admin 64 Jan 25 13:23 tmp
After running the following python script:
import requests
import json
data_json = {
"measurementTime" : 10,
"windSpeed" : 20
}
url = 'http://localhost:8080/php1.php'
r = requests.post(url, data=json.dumps(data_json))
print(r.text)
print(r.status_code)
in the python window, I see:
Got it
200
and in the apache2 window I can see that the json was written to the file:
/usr/local/apache2$ cat tmp/data.txt
{"measurementTime": 10, "windSpeed": 20}
Here's a php script that uses the json data:
<?php
$json = file_get_contents('php://input');
$obj = json_decode($json);
$sum = $obj->measurementTime + $obj->windSpeed;
echo "The sum was: $sum";
?>
In the python window, I see:
The sum was: 30
200
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.