What are the best practices for implementing a Captcha helper class in PHP?

Implementing a Captcha helper class in PHP helps prevent automated bots from submitting forms on your website. This can help reduce spam submissions and protect your website from malicious activities. To implement a Captcha helper class, you can use libraries like Google reCAPTCHA or create your own custom Captcha solution.

<?php
class CaptchaHelper {
    private $secretKey;

    public function __construct($secretKey) {
        $this->secretKey = $secretKey;
    }

    public function verifyCaptcha($response) {
        $url = 'https://www.google.com/recaptcha/api/siteverify';
        $data = array(
            'secret' => $this->secretKey,
            'response' => $response
        );

        $options = array(
            'http' => array(
                'header' => "Content-type: application/x-www-form-urlencoded\r\n",
                'method' => 'POST',
                'content' => http_build_query($data)
            )
        );

        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        $resultJson = json_decode($result);

        return $resultJson->success;
    }
}

// Example usage
$secretKey = 'your_secret_key_here';
$captchaHelper = new CaptchaHelper($secretKey);
$response = $_POST['g-recaptcha-response'];

if ($captchaHelper->verifyCaptcha($response)) {
    // Captcha verification passed, process the form submission
} else {
    // Captcha verification failed, display an error message
}
?>