Open In App

How to send a POST Request with PHP ?

Last Updated : 06 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In web development, sending POST requests is a common practice for interacting with servers and exchanging data. PHP, a versatile server-side scripting language, provides various approaches to accomplish this task. This article will explore different methods to send POST requests using PHP.

Using file_get_contents() and stream_context_create() Functions

This approach involves creating a stream context to configure the request and then using file_get_contents() function to perform the POST request. It is a basic method suitable for simple scenarios.

Example:

PHP




<?php
    
// Define the URL and data
$data = ['key' => 'value'];
  
// Prepare POST data
$options = [
    'http' => [
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => http_build_query($data),
    ],
];
  
// Create stream context
$context  = stream_context_create($options);
  
// Perform POST request
$response = file_get_contents($url, false, $context);
  
// Display the response
echo $response;
  
?>


Here, define the target URL and the data to be sent. Create a stream context with the appropriate POST headers and data. Use file_get_contents with the created context to perform the POST request. Display the received response.

Output:

wdw

Leveraging the cURL Library

The cURL library is a powerful tool for making HTTP requests. PHP provides a curl_init() function to initialize a cURL session, allowing you to configure and execute POST requests with fine-grained control.

Example:

PHP




<?php
  
// Specify the URL and data
$data = ['key' => 'value'];
  
// Initialize cURL session
$ch = curl_init();
  
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  
// Execute cURL session
$response = curl_exec($ch);
  
// Check for cURL errors
if ($response === false) {
    die('Error occurred while fetching the data: ' 
        . curl_error($ch));
}
  
// Close cURL session
curl_close($ch);
  
// Display the response
echo $response;
  
?>


Define the target URL and the data to be sent. Initialize a cURL session using curl_init() function. Set cURL options, including the POST method, POST data, and return transfer option. Execute the cURL session using curl_exec() function. Close the cURL session with curl_close() function. Display the received response.

Output:

wdw

Utilizing the HTTP_Request2 Class

For advanced scenarios, the HTTP_Request2 class from the PEAR package offers a convenient object-oriented approach. It provides features like handling redirects, cookies, and custom headers.

Example:

PHP




<?php
  
// Install PEAR's HTTP_Request2 package
// using Composer
// composer require pear/http_request2
  
// Include Composer autoload
require_once 'vendor/autoload.php';
  
// Specify the URL and data
$data = ['key' => 'value'];
  
// Create HTTP_Request2 object
$request = new HTTP_Request2($url, HTTP_Request2::METHOD_GET);
$request->addPostParameter($data);
  
// Send the request and get the response
try {
    $response = $request->send()->getBody();
    
    // Display the response
    echo $response;
} catch (HTTP_Request2_Exception $e) {
    die('Error occurred while fetching the data: ' 
        . $e->getMessage());
}
  
?>


Install the HTTP_Request2 package using Composer. Include the Composer autoload file. Define the target URL and the data to be sent. Create an HTTP_Request2 object with the POST method and parameters. Send the request using send() and retrieve the response with getBody() Function. Display the received response.

Output:

wdw



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads