Introduction: Why Should a PHP Developer Know How to Send POST Requests?
\n\nImagine this situation: you are writing a website in PHP, and you need to send data to another server. For example, register a user via an external API, send a form to a CRM system, or test your own POST request handler. This is a classic task that every web developer solves.
\n\nThe question "How do I send a POST request with PHP?" on Stack Overflow has garnered hundreds of thousands of views. And this is no coincidence: although PHP is a powerful language, beginners often get confused: which method to choose? Surely you can't just take and send data?
\n\nYou can. And even in four different ways. In this article, we will break down each of them, show real PHP code examples, compare performance, and advise on when to use which method. By the end of the article, you will feel confident sending POST requests in any project.
\n\nMethod 1. The Classic: cURL (Most Flexible and Powerful)
\n\ncURL is a library that is available on almost all hosting providers. It supports HTTPS, cookies, headers, timeouts, and much more. If you need to send a POST request with maximum control — choose cURL.
\n\nSimple Example: Sending a Form with name and email Fields
\n\n<?php\n$url = 'https://api.example.com/submit';\n$data = [\n 'name' => 'Ivan Petrov',\n 'email' => 'ivan@example.com'\n];\n\n$ch = curl_init($url);\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\ncurl_setopt($ch, CURLOPT_POST, true);\ncurl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));\n// If you need to send JSON:\n// curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));\n// curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);\n\n$response = curl_exec($ch);\n$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);\ncurl_close($ch);\n\nif ($httpCode === 200) {\n echo "Success! Server response: " . $response;\n} else {\n echo "Error. HTTP code: " . $httpCode;\n}\n?>\n\nImportant cURL Nuances:
\n- \n
- http_build_query() — converts an array into a string like
name=Ivan&email=ivan@example.com. This is the standard format for forms. \n - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) — so the request result is returned to a variable, rather than being output immediately. \n
- Always check the HTTP response code (200 — success, 4xx/5xx — error). \n
- If the server requires JSON: replace
http_build_querywithjson_encodeand add theContent-Type: application/jsonheader. \n
Method 2. Fast and Simple: file_get_contents with Context
\n\nMany know file_get_contents() as a function for reading files. But few realize that with Stream Context it can send POST requests. This is a great option if you have a simple task and don't want to include third-party libraries.
Example of Sending a POST Request via file_get_contents
\n\n<?php\n$url = 'https://api.example.com/submit';\n$data = [\n 'name' => 'Ivan Petrov',\n 'email' => 'ivan@example.com'\n];\n\n$options = [\n 'http' => [\n 'header' => "Content-Type: application/x-www-form-urlencoded\\r\\n",\n 'method' => 'POST',\n 'content' => http_build_query($data)\n ]\n];\n$context = stream_context_create($options);\n$result = file_get_contents($url, false, $context);\n\nif ($result === false) {\n echo "Error sending request";\n} else {\n echo "Server response: " . $result;\n}\n?>\n\nWhen to Use This Method?
\n- \n
- When you need to quickly test sending data. \n
- When you don't want to write a lot of code with cURL. \n
- Downside: difficult to handle errors (no direct access to the HTTP code). \n