How can PHP be used to send an HTTP request to a server and process the response?

To send an HTTP request to a server and process the response in PHP, you can use the cURL library. cURL allows you to make requests to URLs using a variety of protocols such as HTTP, HTTPS, FTP, and more. You can set options for the request, send data, and handle the response accordingly.

<?php

// Initialize cURL session
$ch = curl_init();

// Set the URL to send the request to
curl_setopt($ch, CURLOPT_URL, 'http://example.com/api');

// Set additional options if needed
// curl_setopt($ch, CURLOPT_POST, 1);
// curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['key' => 'value']));

// Set option to receive the response as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

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

// Close the cURL session
curl_close($ch);

// Process the response
if ($response === false) {
    echo 'Error: ' . curl_error($ch);
} else {
    echo 'Response: ' . $response;
}

?>