PHP code example of friendlycaptcha / sdk

1. Go to this page and download the library: Download friendlycaptcha/sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

friendlycaptcha / sdk example snippets


use FriendlyCaptcha\SDK\{Client, ClientConfig}

$config = new ClientConfig();
$config->setAPIKey("<YOUR API KEY>")->setSitekey("<YOUR SITEKEY (optional)>");

// You can also specify which endpoint to use, for example `"global"` or `"eu"`.
// $config->setApiEndpoint("eu")

$captchaClient = new Client($config)

function handleLoginRequest() {
    global $captchaClient;

    $captchaResponse = isset($_POST["frc-captcha-response"]) ? $_POST["frc-captcha-response"] : null;
    $result = $captchaClient->verifyCaptchaResponse($captchaResponse);

    if (!$result->wasAbleToVerify()) {
        if ($result->isClientError()) {
            // ALERT: your website is NOT PROTECTED because of a configuration error.
            // Send an alert to yourself, check your API key (and sitekey).
            error_log("Failed to verify captcha response because of configuration problem: " . print_r($result->getResponseError()));
        } else {
            // Something else went wrong, maybe there is a connection problem or the API is down.
            error_log("Failed to verify captcha response: " . print_r($result->getErrorCode()));
        }
    }

    if ($result->shouldReject()) {
        // The captcha was not OK, show an error message to the user
        echo "Captcha anti-robot check failed, please try again.";
        return;
    }

    // The captcha is accepted, handle the request:
    loginUser($_POST["username"], $_POST["password"]);
}

function getRiskIntelligence() {
    global $frcClient;

    $token = isset($_POST["frc-risk-intelligence-token"]) ? $_POST["frc-risk-intelligence-token"] : null;
    $result = $frcClient->retrieveRiskIntelligence($token);

    if ($result->wasAbleToRetrieve()) {
        // Risk Intelligence token is valid and data was retrieved successfully.
        if ($result->isValid()) {
            // Token was invalid or expired. 
            $response = $result->getResponse();
            echo "Risk Intelligence data", $response->data;
        } else {
            $error = $result->getResponseError();
            error_log("Error:", $error);
        }
    } else {
        // Network issue or configuration problem.
        if ($result->isClientError()) {
            error_log("Configuration error - check your API key");
        } else {
            error_log("Network issue or service temporarily unavailable");
        }
    }
}
shell
mkdir -p bin
php -r "copy('https://getcomposer.org/installer', './bin/composer-setup.php');"
# You can omit `--2.2 LTS` if you are using a more recent PHP version than 7.2
php bin/composer-setup.php --install-dir=bin --2.2 LTS
console
$ docker run --rm -it --network host -v $PWD:/php -w /php php:7.1-alpine /bin/sh
/php # apk update && apk add git