How can PHP scripts pass data to a URL using POST method?

To pass data to a URL using the POST method in PHP, you can use the cURL library to make an HTTP POST request. This involves setting the CURLOPT_POST option to true, setting the CURLOPT_POSTFIELDS option to the data you want to send, and specifying the URL to which you want to send the data.

// Data to be sent
$data = array(
    'key1' => 'value1',
    'key2' => 'value2'
);

// URL to send the data
$url = 'https://example.com/api';

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

// Set the CURLOPT_POST option to true
curl_setopt($ch, CURLOPT_POST, true);

// Set the CURLOPT_POSTFIELDS option with the data to be sent
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

// Execute the cURL session
$response = curl_exec($ch);

// Close cURL session
curl_close($ch);

// Handle the response
echo $response;