Examples

If you are using the Akismet API, it is recommended you build a system to enter data back into the API either by submitting missed spam, or by submitting false positives. While not strictly required, passing data back using the API will allow the service to learn from its mistakes, and will ensure the service stays relevant for your needs.

Handling User-Submitted Comments

<?php

require_once 'Services/Akismet2.php';
require_once 
'Services/Akismet2/Comment.php';

$comment = new Services_Akismet2_Comment(array(
    
'comment_author'       => 'Test Author',
    
'comment_author_email' => 'test@example.com',
    
'comment_author_url'   => 'http://example.com/',
    
'comment_content'      => 'Hello, World!'
));

try {
    
$apiKey  'AABBCCDDEEFF';
    
$akismet = new Services_Akismet2('http://blog.example.com/'$apiKey);
    if (
$akismet->isSpam($comment)) {
        
// rather than simply ignoring the spam comment, it is recommended
        // to save the comment and mark it as spam in case the comment is a
        // false positive.
    
} else {
        
// save comment as normal comment
    
}
} catch (
Services_Akismet2_InvalidApiKeyException $keyException) {
    echo 
'Invalid API key!';
} catch (
Services_Akismet2_HttpException $httpException) {
    echo 
'Error communicating with Akismet API server: ' .
        
$httpException->getMessage();
} catch (
Services_Akismet2_InvalidCommentException $commentException) {
    echo 
'Specified comment is missing one or more required fields.' .
        
$commentException->getMessage();
}

?>

Marking a Missed Comment as Spam

<?php

require_once 'Services/Akismet2.php';
require_once 
'Services/Akismet2/Comment.php';

$comment = new Services_Akismet2_Comment(array(
    
'comment_author'       => 'Test Author',
    
'comment_author_email' => 'test@example.com',
    
'comment_author_url'   => 'http://example.com/',
    
'comment_content'      => 'Hello, World!'
));

try {
    
$apiKey  'AABBCCDDEEFF';
    
$akismet = new Services_Akismet2('http://blog.example.com/'$apiKey);
    
$akismet->submitSpam($comment);
} catch (
Services_Akismet2_InvalidApiKeyException $keyException) {
    echo 
'Invalid API key!';
} catch (
Services_Akismet2_HttpException $httpException) {
    echo 
'Error communicating with Akismet API server: ' .
        
$httpException->getMessage();
} catch (
Services_Akismet2_InvalidCommentException $commentException) {
    echo 
'Specified comment is missing one or more required fields.' .
        
$commentException->getMessage();
}

?>

Marking a Comment as a False Positive

<?php

require_once 'Services/Akismet2.php';
require_once 
'Services/Akismet2/Comment.php';

$comment = new Services_Akismet2_Comment(array(
    
'comment_author'       => 'Test Author',
    
'comment_author_email' => 'test@example.com',
    
'comment_author_url'   => 'http://example.com/',
    
'comment_content'      => 'Hello, World!'
));

try {
    
$apiKey  'AABBCCDDEEFF';
    
$akismet = new Services_Akismet2('http://blog.example.com/'$apiKey);
    
$akismet->submitFalsePositive($comment);
} catch (
Services_Akismet2_InvalidApiKeyException $keyException) {
    echo 
'Invalid API key!';
} catch (
Services_Akismet2_HttpException $httpException) {
    echo 
'Error communicating with Akismet API server: ' .
        
$httpException->getMessage();
} catch (
Services_Akismet2_InvalidCommentException $commentException) {
    echo 
'Specified comment is missing one or more required fields.' .
        
$commentException->getMessage();
}

?>
Submits a comment as incorrectly detected spam to the Akismet server. (Previous) Services_Amazon_S3 (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

There are no user contributed notes for this page.