Zf3 google Re-captcha problem



hi there, I have a problem using ZendService\ReCaptcha\ReCaptcha in my Contact form. I already registered in https://www.google.com/recaptcha/admin.
I also installed ZendService\ReCaptcha\ReCaptcha from https://github.com/zendframework/ZendService_ReCaptcha and here is my composer.json code:

"name": "zendframework/skeleton-application",
"description": "Skeleton Application for Zend Framework zend-mvc applications",
"type": "project",
"license": "BSD-3-Clause",
"keywords": [
"homepage": "http://framework.zend.com/",
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
    "php": "^5.6 || ^7.0",
    "zendframework/zend-component-installer": "^1.0 || ^0.7 || ^1.0.0-dev@dev",
    "zendframework/zend-mvc": "^3.0.1",
    "zfcampus/zf-development-mode": "^3.0",
    "zendframework/zend-db": "^2.8.1",
    "zendframework/zend-mvc-form": "^1.0",
    "zendframework/zend-json": "^3.1",
    "zendframework/zend-session": "^2.7.1",
    "zendframework/zend-navigation": "^2.8",
    "zendframework/zend-paginator": "^2.7",
    "zendframework/zend-permissions-acl": "^2.6",
    "tasmaniski/zend-layout-change": "^1.0",
    "zendframework/zend-captcha": "^2.7",
    "zendframework/zendservice-recaptcha": "*",
    "zendframework/zend-http": "^2.5.4"
"autoload": {
    "psr-4": {
        "Application\\": "module/Application/src/",
        "Blog\\": "module/Blog/src/",
        "PageBuilder\\": "module/PageBuilder/src",
        "Contact\\": "module/Contact/src",
        "ZendService\\ReCaptcha\\": "src/"
"autoload-dev": {
    "psr-4": {
        "ApplicationTest\\": "module/Application/test/"
"extra": {
    "branch-alias": {
        "dev-master": "3.0-dev",
        "dev-develop": "3.1-dev"
,"repositories": [
        "type": "composer",
        "url": "https://packages.zendframework.com/"
"scripts": {
    "cs-check": "phpcs",
    "cs-fix": "phpcbf",
    "development-disable": "zf-development-mode disable",
    "development-enable": "zf-development-mode enable",
    "development-status": "zf-development-mode status",
    "post-create-project-cmd": [
    "serve": "php -S -t public public/index.php",
    "test": "phpunit"

here is my Contact\Controller\IndexController for using re-captcha:

        public function indexAction()
            $recaptcha = new \ZendService\ReCaptcha\ReCaptcha(
             return new ViewModel

and here the index.phtml code:

            echo $this->recaptcha->getHTML();
                $recaptcha_challenge =$_POST['recaptcha_challenge_field'];
                $recaptcha_response = $_POST['recaptcha_response_field'];
                    isset($recaptcha_response) ||
                    $result = $this->recaptcha->verify(
                    if (!$result->isValid())
                        echo $result->getErrorCodes();

but after all my form is submitted without filling re-captcha part. and there is no error. what should I do?


I haven’t used ZendService_ReCaptcha myself so I can’t help you with that. But I do hope those are not your real recaptcha keys, although they don’t look like the official test keys. You have a public and a secret key for a reason!

EDIT: Removed the keys for you. I suggest to create a new set and don’t post those on a public forum.


the work is done by adding ZendService\ReCaptcha . then back to Controller and instantiate the class using $recaptcha = new \ZendService\ReCaptcha\ReCaptcha($pubKey, $privKey); the next step will be pass $recaptcha to specified ViewModel and in the view(in my project index.phtm) render the html recaptcha field using recaptcha->getHTML(). when a user fill out the recaptcha in the controller we receive **_POST[‘g-recaptcha-response’]** . check the validation in Controller:

$result =$recaptcha->verify( $_POST['g-recaptcha-response'])
 throw new \Exception('recaptcha is not valid');

and that’s all the following code is your specific logic to handle form submission!