The Comment Class

Overview

The class Services_Akismet2_Comment represents a comment on a website. All Akismet API methods take a comment as the first parameter. The comment class contains content-related information such as the comment text, author name, author email and Web link; as well as server-related information such as the HTTP referer, timestamp and IP address. The accuracy provided by the Akismet API increases as more fields are included in the comment.

Required Fields

There are two required fields:

  • User Agent (the browser used to submit the comment), and
  • User IP (IP address from which the comment was submitted).

The blog URL, as referenced in the Akismet API documentation, is specified in the Services_Akismet2 constructor and does not need to be specified in the comment object.

Auto-Setting Server Fields

When checking if a comment is spam, it is possible to set the required fields, and several other server-related fields automatically. To do so, use the second parameter of the Services_Akismet2::isSpam() method. When using this parameter, usually only the content-related fields need to be specified manually.

Only auto-set server-related fields on actual comments submitted in real-time. If you check comments using an external system, you run the risk of submitting your own server information as spam. Instead, save the server information in the database and set it manually using the Services_Akismet2::setField() method.

Examples

There are two ways to specify fields in the comment object: setter methods, and in the constructor. As a shortcut, Services_Akismet2 methods that require a comment also accept an array of fields. For example:

<?php

require_once 'Services/Akismet2/Comment.php';

// set in constructor
$comment = new Services_Akismet2_Comment(array(
    
'comment_author'       => 'Test User',
    
'comment_author_email' => 'test@example.com',
    
'comment_author_url'   => 'http://myblog.example.com',
    
'comment_content'      => 'Buy V1agra!'
));

// using setter methods
$comment = new Services_Akismet2_Comment();
$comment->setAuthor('Test User')
        ->
setAuthorEmail('test@example.com')
        ->
setAuthorUrl('http://myblog.example.com')
        ->
setContent('Buy V1agra!');

// using an array as a shortcut
$akismet->isSpam(array(
    
'comment_author'       => 'Test User',
    
'comment_author_email' => 'test@example.com',
    
'comment_author_url'   => 'http://myblog.example.com',
    
'comment_content'      => 'Buy V1agra!'
));

?>