How can PHP be used to send data to a foreign website via POST method?

To send data to a foreign website via the POST method using PHP, you can use the cURL library. This library allows you to make HTTP requests to external servers and send data in the request body. You can set the CURLOPT_POST option to true and pass the data you want to send in the CURLOPT_POSTFIELDS option.

<?php
$url = 'https://example.com/api'; // URL of the foreign website
$data = array(
    'key1' => 'value1',
    'key2' => 'value2'
);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if($response === false){
    echo 'Error: ' . curl_error($ch);
} else {
    echo 'Data sent successfully!';
}

curl_close($ch);
?>