How to Send a POST Request in PHP: 4 Working Methods with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Send a POST Request in PHP: From Beginner to Pro



Have you ever encountered a situation where your PHP script needs to "talk" to another server? Send form data, call an external API, or just check if a remote endpoint is working? If so, you've probably wondered: "How do I send a POST request with PHP?". This is one of the most popular questions on Stack Overflow, and for good reason — server-to-server interaction is the foundation of the modern web.



In this article, we'll cover four proven ways to send a POST request in PHP. You'll learn how to do it using cURL, file_get_contents, stream_context_create, and the powerful Guzzle HTTP client. We won't just show code — we'll explain which method to choose for your task and how to avoid common mistakes.



Let's go! 🚀



What is a POST Request and Why Do You Need It in PHP?



POST is one of the HTTP protocol methods designed for sending data to a server. Unlike GET, where parameters are visible in the URL, POST transmits data in the request body. This is convenient when you need to:



  • Submit a registration or order form;
  • Call a REST API (e.g., create a new record in a CRM);
  • Upload a file to a remote server;
  • Authenticate with a third-party service.


PHP doesn't have a built-in "sendPostRequest()" function, but it has several flexible tools that allow you to do this. Let's look at each of them.



Method 1: cURL — The Universal Soldier



cURL (Client URL Library) is arguably the most popular way to send HTTP requests in PHP. It supports a ton of protocols, can handle SSL, cookies, timeouts, and proxies. If you're writing something more serious than a learning script — take a closer look at cURL.



Simple POST Request with cURL



Here's a basic example that sends a POST request to the JSONPlaceholder test server and outputs the response:



// 1. Create a new cURL resource$ch = curl_init();

// 2. Set the endpoint URLcurl_setopt($ch, CURLOPT_URL, "https://jsonplaceholder.typicode.com/posts");

// 3. Specify that this is a POST requestcurl_setopt($ch, CURLOPT_POST, 1);

// 4. Pass the data (PHP array is automatically converted to a query string)curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([ 'title' => 'My first post', 'body' => 'This is the post body', 'userId' => 1]));

// 5. Return the result instead of outputting it directlycurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// 6. Execute the request$response = curl_exec($ch);

// 7. Check for errorsif (curl_errno($ch)) { echo 'cURL error: ' . curl_error($ch);} else { // 8. Close the connection curl_close($ch); // 9. Output the result echo $response;}


POST with JSON and Headers



Modern APIs most often work with JSON. Here's how to send JSON data and set the correct Content-Type header:



$data = [    'email' => 'user@example.com',    'password' => 'secret123'];

$ch = curl_init('https://api.example.com/login');curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Accept: application/json']);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Get the HTTP response code

curl_close($ch);

if ($httpCode === 200) { $result = json_decode($response, true); print_r($result);} else { echo "Error: HTTP $httpCode";}


Why cURL? It gives you full control: you can set a timeout (CURLOPT_TIMEOUT), follow redirects (CURLOPT_FOLLOWLOCATION), send a file (CURLFile), and even work with FTP. The only downside is that the code can be verbose.



Meth

Blogs

Book Recommendations