How to Send a POST Request Using PHP
PHP offers two primary ways to send POST requests: using the PHP cURL library or via a native stream-based method. The cURL library provides extensive customization options for handling POST requests but involves more setup. On the other hand, PHP’s native streaming method is simpler to implement but has limited functionality.
In this guide, we’ll cover how to send a POST request using the PHP cURL library and explore the CURL-less alternative with a detailed explanation later. You can also try our live PHP POST request example online for practical insights.
Understanding the PHP cURL Library
The PHP cURL library, built on the libcurl framework by Daniel Stenberg, enables server interaction over various protocols such as HTTP, HTTPS, and FTP. It supports features like:
- HTTPS certificates
- HTTP methods (GET, POST, PUT, PATCH, etc.)
- FTP file uploads (alternative: PHP FTP extension)
- HTTP form submissions
- Proxy usage
- Cookies and user authentication
To use PHP cURL, ensure that your PHP version is compiled with cURL support, and libcurl version 7.10.5 or higher is installed. Additionally, the curl.cainfo setting in your php.ini file may affect the behavior of cURL functions.
What is the HTTP POST Request Method?
The HTTP POST method is commonly used for submitting data to a server. Examples include:
- Submitting login forms
- Uploading files (images, PDFs, etc.)
- Sending JSON or XML data
Unlike GET requests, POST sends data in the HTTP request body or URL parameters. Headers like Content-Type and Content-Length help servers interpret and handle the data appropriately.
Sending POST Requests with PHP cURL
To send a POST request using PHP cURL, follow these steps:
- Initialize cURL using curl_init().
- Set request options with curl_setopt().
- Use CURLOPT_URL to define the target URL.
- Use CURLOPT_POST to specify the POST method.
- Use CURLOPT_POSTFIELDS to provide the POST data.
- Execute the request and fetch the response.
Here’s a complete example:
<?php
$url = "https://example.com/api";
$data = array("key1" => "value1", "key2" => "value2");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
echo $response;
?>
Sending POST Requests Without cURL
If cURL is unavailable, you can use PHP’s stream_context_create() function to send POST requests. While less flexible, it’s sufficient for basic use cases.
Here’s how it works:
- Create an $options array with HTTP headers, method, and content.
- Pass the $options to stream_context_create().
- Use file_get_contents() to fetch the server’s response.
Example:
<?php
$url = "https://example.com/api";
$data = array("key1" => "value1", "key2" => "value2");
$options = array(
"http" => array(
"header" => "Content-Type: application/x-www-form-urlencoded\r\n",
"method" => "POST",
"content" => http_build_query($data),
),
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
echo $response;
?>
Sending JSON Data with PHP
To send JSON data in a POST request:
- Set the Content-Type header to application/json.
- Provide the JSON-encoded data in the request body.
Example:
<?php
$data = json_encode(array("key" => "value"));
$headers = array("Content-Type: application/json");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
?>
Sending XML Data with PHP
To send XML data, use a Content-Type: application/xml header and include the XML payload in the request body.
Submitting HTML Forms with PHP
HTML forms use application/x-www-form-urlencoded as the default Content-Type. Data is sent as key-value pairs in the request body:
key=value&key2=value2
With the methods outlined above, you can efficiently handle POST requests using PHP, whether through cURL or its alternative stream-based approach.