<?php

class ReCaptcha {
    private $secret;

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

    public function verify($response, $remoteIp) {
        $url = 'https://www.google.com/recaptcha/api/siteverify';
        $data = [
            'secret' => $this->secret,
            'response' => $response,
            'remoteip' => $remoteIp
        ];

        $options = [
            'http' => [
                '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);
        return json_decode($result);
    }
}
