What are some best practices for securely incorporating external scripts, like a Whois query, into a PHP-based online shop?

When incorporating external scripts like a Whois query into a PHP-based online shop, it is crucial to sanitize user input to prevent SQL injection and other security vulnerabilities. Additionally, it is recommended to use prepared statements to interact with the database securely. Finally, consider implementing input validation to ensure that only valid data is processed by the script.

// Sanitize user input
$domain = filter_var($_POST['domain'], FILTER_SANITIZE_STRING);

// Prepare the SQL statement using prepared statements
$stmt = $pdo->prepare("SELECT * FROM domains WHERE domain = :domain");
$stmt->bindParam(':domain', $domain, PDO::PARAM_STR);
$stmt->execute();

// Input validation to ensure valid data is processed
if ($stmt->rowCount() > 0) {
    // Process the Whois query
} else {
    echo "Invalid domain";
}