I'm having trouble while sending data to PHP script through python
if i try with browser then data is posting but if i try with python script it is not posting any data and no error is shown
i have tried using headers, get instead of post
PHP:
$q = @$_GET['q'];
if($q) {
$sql = "INSERT INTO data VALUES ('', '".$q."')";
if (mysqli_query($conn, $sql)) {
echo "Posted";
} else {
echo "cannot post";
}
}
PYTHON:
import requests
payload = {'q': 'some-data'}
r = requests.post('http://example.com/store.php', params=payload)
Your Python requests
method needs to match the one used by your PHP script to process the data.
You are currently using POST method - requests.post()
- in your Python script, while trying to retrieve the data sent in the query string on the PHP end with $_GET(). That won't work.
You need to stay consistent...
In your python script use a 'GET' request:
r = requests.get('http://example.com/store.php', params=payload)
You can then process the data with PHP using:
$q = $_GET['q'];
By the way, some free advice, do not use @ to suppress errors in PHP, it's bad practice.