Bootstrap captcha not verifying the response

I want to implement Google ReCaptcha for my project, and I have been following the tutorial in, using the default keys for testing in local obtained from this site.

I think everything in the front is going well, because it lets me click the captcha and does not let me pass if I don't click it. Here is the HTML code I added following the tutorial:

    <form id="form_register" method="post" role="form" name="form_register"> 
      <!-- Some inputs -->
       <div class="form-group">
             <div class="g-recaptcha" data-sitekey="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI" data-callback="verifyRecaptchaCallback" data-expired-callback="expiredRecaptchaCallback"></div>
             <input class="form-control d-none" data-recaptcha="true" required data-error="Please complete the Captcha">
             <div class="help-block with-errors"></div>
       <button type="submit" class="mixpanel_link_registro btn btn-lg btn-primary btn-extra-mobile w-100"> Save </button>

And this is the JS code:

        <script src=''></script>
        <script src="validator.js"></script>

          $(function () {

            window.verifyRecaptchaCallback = function (response) {

            window.expiredRecaptchaCallback = function () {


            $('#form_register').on('submit', function (e) {
                if (!e.isDefaultPrevented()) {
                  var url = "recaptcha.php";
                      type: "POST",
                      url: url,
                      data: $(this).serialize(),
                      success: function (data) {
                        var messageAlert = 'alert-' + data.type;
                        var messageText = data.message;

                        var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';
                        if (messageAlert && messageText) {
                      error: function (xhr, ajaxOptions, thrownError) {
                  return false;

However, when it goes to my recaptcha.php file through ajax, it doesn't validate my response. The PHP code that I used is this:


// ReCaptch Secret
$recaptchaSecret = '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe';

error_reporting(E_ALL & ~E_NOTICE);

try {
    if (!empty($_POST)) {
       // validate the ReCaptcha, if something is wrong, we throw an Exception,
       // i.e. code stops executing and goes to catch() block

       if (!isset($_POST['g-recaptcha-response'])) {
           throw new \Exception('ReCaptcha is not set.');

       $recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost());

       // we validate the ReCaptcha field together with the user's IP address
       $server = $_SERVER['REMOTE_ADDR'] . ":" . $_SERVER['SERVER_PORT'];
       $response = $recaptcha->verify($_POST['g-recaptcha-response'], $server);

       if (!$response->isSuccess()) {
           throw new \Exception('ReCaptcha was not validated.');

 } catch (\Exception $e) {
   $_SESSION["error"] = $e;

Printing the recaptcha variable gives me the following output:

__PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => ReCaptcha\ReCaptcha [secret:ReCaptcha\ReCaptcha:private] => 6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe [requestMethod:ReCaptcha\ReCaptcha:private] => __PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => ReCaptcha\RequestMethod\CurlPost [curl:ReCaptcha\RequestMethod\CurlPost:private] => __PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => ReCaptcha\RequestMethod\Curl ) ) )

And I get the exception that says: Recaptcha has not been validated. I would like to know what can I do to be able to validate my captchas.

Thanks in advance.