POST requests in PHP with cURL: 2025 Guide

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

PHP, cURL and HTTP POST: How to Send Data Like a Pro in 2025

\n\n

If you've ever written PHP code that needed to "talk" to another server — submit a form, call an API, or just transfer data — you've likely encountered cURL. It's not just a library; it's a Swiss Army knife for HTTP requests. And although in 2025 we have more modern tools like Guzzle or Symfony HttpClient, cURL remains the foundation that every PHP developer needs to know.

\n\n

The question "PHP, cURL, and HTTP POST example?" on Stack Overflow has gathered thousands of views. Why? Because it's that one task that appears in every second project: from integrating with payment systems to sending data to a CRM. Let's figure out how to do it correctly, securely, and elegantly.

\n\n

What is cURL and Why Do You Need It in PHP?

\n\n

cURL (Client URL) is a powerful library that allows your PHP script to act as an HTTP client. Imagine your code is a browser without an interface. It can open pages, submit forms, download files, and even work with cookies.

\n\n

In 2025, cURL remains relevant because:

\n
    \n
  • It's built into PHP (no need to install additional packages)
  • \n
  • It works faster than some "heavy" libraries
  • \n
  • It gives you full control over every aspect of the request
  • \n
  • It supports all modern protocols: HTTP/2, HTTPS, FTP, and even IMAP
  • \n
\n\n

Basic Example: Sending a POST Request with cURL

\n\n

Let's start with the classic. Here's minimal working code that sends a POST request to a server:

\n\n
<?php\n// URL to send data to\n$url = 'https://api.example.com/submit';\n\n// Data to send (associative array)\n$postData = [\n    'name' => 'Ivan Petrov',\n    'email' => 'ivan@example.com',\n    'message' => 'Hello from 2025!'\n];\n\n// Initialize cURL\n$ch = curl_init($url);\n\n// Request settings\ncurl_setopt_array($ch, [\n    CURLOPT_POST => true,                          // This is a POST request\n    CURLOPT_POSTFIELDS => http_build_query($postData), // Data as a string\n    CURLOPT_RETURNTRANSFER => true,                // Return response as string\n    CURLOPT_HEADER => false,                       // Do not return headers\n    CURLOPT_TIMEOUT => 30,                         // Timeout 30 seconds\n    CURLOPT_SSL_VERIFYPEER => true,                // Verify SSL certificate\n]);\n\n// Execute the request\n$response = curl_exec($ch);\n\n// Check for errors\nif (curl_errno($ch)) {\n    echo 'cURL Error: ' . curl_error($ch);\n} else {\n    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n    echo "HTTP response code: $httpCode\\n";\n    echo "Response body: $response";\n}\n\n// Close the connection\ncurl_close($ch);\n?>
\n\n

Notice the use of http_build_query(). This is a convenient function that converts an array into a URL-encoded string. Without it, cURL might process the data incorrectly.

\n\n

Sending JSON Instead of Form Data

\n\n

In 2025, most APIs work with JSON rather than classic POST forms. Here's how to adapt our example:

\n\n
<?php\n$url = 'https://api.example.com/v2/users';\n$jsonData = json_encode([\n    'username' => 'developer_2025',\n    'password' => 'secret_password',\n    'role' => 'admin'\n]);\n\n$ch = curl_init($url);\ncurl_setopt_array($ch, [\n    CURLOPT_POST => true,\n    CURLOPT_POSTFIELDS => $jsonData,  // Send raw JSON\n    CURLOPT_HTTPHEADER => [           // Important: specify headers\n        'Content-Type: application/json',\n        'Content-Length: ' . strlen($jsonData),\n        'Accept: application/json'\n    ],\n    CURLOPT_RETURNTRANSFER => true,\n    CURLOPT_TIMEOUT => 15,\n]);\n\n$response = curl_exec($ch);\n$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);\ncurl_close($ch);\n\nif ($htt                

Blogs

Book Recommendations