How do I send a POST request with PHP?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Sending a POST request in PHP usually means making an HTTP request to another server or API and putting data in the request body. The most practical tool for this is cURL because it gives you control over headers, request body format, timeouts, and error handling.
Sending a JSON POST request with cURL
A common API pattern is JSON over HTTP. In PHP, the core steps are: encode the payload, set cURL options, execute the request, and inspect the response.
This is the pattern most developers reach for when calling REST APIs.
Sending form data instead of JSON
Not every server expects JSON. Some endpoints want regular form-encoded data:
The difference is in the body format and the matching Content-Type header.
Reading the response safely
A POST request is not complete until you inspect the response properly. At minimum, check:
- whether cURL execution failed,
- the HTTP status code,
- and whether the response body is valid JSON if the API promises JSON.
That prevents silent failures where the request technically completes but the remote server rejects the payload.
A simpler stream-context alternative
If cURL is unavailable, PHP can also send a POST request with stream contexts:
This works for simple cases, but cURL is still the better default because it offers better diagnostics and more features.
Common Pitfalls
The most common mistake is sending JSON data without the Content-Type: application/json header. Many APIs will reject or misread the body if the header does not match the payload format.
Another issue is forgetting CURLOPT_RETURNTRANSFER. Without it, the response may be printed directly instead of being captured in your PHP variable.
Be careful with SSL and timeouts too. Disabling SSL verification to make a local test work is a bad habit, and omitting timeouts can leave the process hanging indefinitely.
Finally, do not assume every successful network call means a successful application result. An HTTP 400 or 500 response still means the request reached the server, so always inspect the status code. Logging both the request format and the response body during development can save a lot of time when an API rejects your payload for a schema mismatch.
Summary
- Use cURL for most PHP POST requests.
- Match the body format and the
Content-Typeheader. - Capture the response and check for cURL errors and HTTP status codes.
- Use
http_build_query()for form data andjson_encode()for JSON payloads. - Stream contexts work for simple cases, but cURL is usually more robust.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.