What is a secure and efficient way to verify that a request from one server to another in PHP is genuine and not from a random source?

To verify that a request from one server to another in PHP is genuine and not from a random source, you can use a combination of encryption and authentication methods. One common approach is to use JSON Web Tokens (JWT) to securely transmit information between servers. By encrypting the payload with a shared secret key, you can ensure that the request is authentic and has not been tampered with during transmission.

// Server 1 (Sending server)
$payload = array(
    "data" => "example data",
    "timestamp" => time()
);

$jwt = JWT::encode($payload, 'your_secret_key');

// Send the JWT to Server 2

// Server 2 (Receiving server)
$received_jwt = $_POST['jwt'];

try {
    $decoded = JWT::decode($received_jwt, 'your_secret_key', array('HS256'));
    // Verify the payload data and timestamp
    if ($decoded->data === "example data" && ($decoded->timestamp + 60) >= time()) {
        // Request is genuine
        echo "Request verified!";
    } else {
        // Request is not genuine
        echo "Request not verified!";
    }
} catch (Exception $e) {
    // Invalid token
    echo "Invalid token!";
}