============
A PHP client for the CleanTalk anti-spam API
If you find this project useful, please consider starring it on GitHub ⭐. It helps us improve and maintain the project.
- PHP 5.6 or later
- cURL support
This PHP library provides invisible spam protection for websites, registration forms, and comment sections. The CleanTalk API is a CAPTCHA alternative that detects spam without interrupting your users.
When a user submits a form, the library securely sends the form data to CleanTalk's cloud servers for analysis. CleanTalk then returns a real-time verdict identifying the submission as legitimate or spam. Your application decides how to handle that verdict.
| Feature | CleanTalk Anti-Spam | Traditional CAPTCHA |
|---|---|---|
| User Interaction | 100% invisible to users | Requires solving puzzles or clicks |
| Form Compatibility | Works with any PHP form | Often requires additional scripts |
| Speed | Instant cloud check | Slower due to user interaction |
| Accessibility | No visual tests | Can be difficult for screen-reader users |
CleanTalk is a PHP spam filter and CAPTCHA-free alternative that protects your forms without adding friction for users.
Setup takes only a few minutes.
Install the SDK with Composer:
composer require cleantalk/php-antispamAlternatively, download the ZIP archive and extract it into your project directory.
First, copy your API key from your CleanTalk dashboard.
Make it available to PHP as an environment variable:
| Name | Value |
|---|---|
CLEANTALK_API_KEY |
Your CleanTalk API key |
On managed hosting, add it in the Environment variables or Secrets section of your hosting control panel, then restart the application if required.
If your application already loads a .env file, add:
CLEANTALK_API_KEY=your-api-keyPHP does not load .env files by itself; this option works only when your application or framework includes a dotenv loader. Do not put the API key in your PHP files or commit it to source control.
Add the handler to your PHP code:
$api_key = getenv('CLEANTALK_API_KEY');
if (empty($api_key)) {
throw new RuntimeException('CLEANTALK_API_KEY is not configured');
}
$email_field = $_POST['email']; // Get this value from your form.
$cleantalk_antispam = new CleantalkAntispam($api_key, $email_field);
// Set additional parameters here.
$api_result = $cleantalk_antispam->handle();The library collects frontend data used for bot detection.
<script src="https://fd.cleantalk.org/ct-bot-detector-wrapper.js" defer></script>Enable the event token in your PHP handler:
...
// Set additional parameters here.
$cleantalk_antispam->setEventTokenEnabled(1);
...For example, stop processing when CleanTalk identifies a submission as spam:
if ($api_result && $api_result->allow === 0) {
die('Blocked. Spam protection OK. Reason: ' . $api_result->comment);
// Or add your own actions, logs, or messages.
}To identify possible configuration problems, log the improvement suggestions after calling handle():
// Troubleshooting: log improvement suggestions.
error_log($cleantalk_antispam->whatsWrong(true));See the complete form-handler example for context.
If you have questions, open a GitHub issue or contact us through our ticket system.
See php-uni for a universal solution for CMS platforms and custom websites.
Learn more about CleanTalk as a reCAPTCHA alternative.
