Sending data using POST in Python to PHP

Sending data using POST in Python to PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Debugging Python-to-POST PHP Data Transfer Failures

When a Python script sends data to a PHP endpoint and that data vanishes, the issue almost never lies in the server-side logic itself. Instead, it is almost invariably a client-side configuration error or a protocol mismatch. In your specific scenario, where $_POST['data'] returns an undefined index while $_GET['data'] works perfectly, the PHP code is functioning correctly; it is simply waiting for a request that matches its expected input method.

The core of the problem is ensuring that the HTTP verb used by Python matches the superglobal array accessed in PHP. If your PHP script uses $data=$_POST['data'];, it strictly requires an HTTP POST request containing a Content-Type header indicating form data or JSON, depending on how you parse the payload. If Python sends this as a GET request, or if it sends a POST request without the correct headers and body encoding, the PHP superglobal will remain empty.

Consider the following Python implementation using the standard library urllib:

import urllib.request
import urllib.parse

data = "Hello from Python"
url = "http://localhost/mine.php"

# Correct POST implementation
payload = data.encode('utf-8')
headers = {
    'Content-Type': 'application/x-www-form-urlencoded'
}

req = urllib.request.Request(url, data=payload, headers=headers)

with urllib.request.urlopen(req) as response:
    print(response.read().decode('utf-8'))

In this example, the urllib.request.Request object is explicitly configured to send a POST request. Crucially, the Content-Type header must be set to application/x-www-form-urlencoded. This tells the server that the body of the request contains key-value pairs separated by ampersands (&) and equals signs (=), which PHP expects for standard form submissions. Without this header, or if the data is sent as raw bytes without proper encoding, PHP's default parsing logic may fail to populate $_POST.

Alternatively, you can use the requests library, which handles these details more intuitively:

import requests

data = {'data': 'Hello from Python'}
url = "http://localhost/mine.php"

response = requests.post(url, data=data)
print(response.text)

Here, passing a dictionary to the data parameter automatically sets the correct content type and encodes the body correctly. This approach eliminates many of the manual configuration steps required by the standard library.

A common pitfall involves mixing up JSON payloads with form-encoded data. If your Python script sends JSON (e.g., using requests.post(url, json={"data": "value"})), you must ensure your PHP script is prepared to read it. Standard $_POST['data'] will not capture a JSON payload; instead, you would need to read the raw input stream using file_get_contents('php://input') and decode the JSON string manually. If you stick to form-encoded data as shown in your source code, ensure Python is sending exactly that format.

Another frequent mistake is forgetting to encode special characters. If your data contains spaces or non-ASCII characters, they must be URL-encoded before being sent via application/x-www-form-urlencoded. For instance, a space should become %20. If you send raw strings containing spaces without encoding, the server might receive them as part of the query string in some misconfigured scenarios, or the parsing might split the data incorrectly.

Finally, verify that your web server is actually receiving the request. Sometimes firewalls or load balancers intercept requests based on headers or methods, dropping POST traffic if it doesn't match expected patterns. However, given that the GET method works, the server itself is reachable and capable of processing the specific endpoint. The failure to receive data in $_POST confirms that the request body is either not being sent, not being formatted correctly, or the wrong HTTP verb is being used by the Python client.

Our network