The
Akismet API documentation describes the low level interface. Its intended audience is authors of code such as the Akismet PHP4 class used in R3-born. As such, it contains some information you don't need to know or be concerned with, but it may help you understand what the Akismet class is doing.
Using the Class
The Akismet class takes care of all the connection and communications with the Akismet server and gives you a simpler high level interface.
Example 1, submitting a comment to be checked:
Код: |
include 'class_akismet4.php';
// load array with comment data
$comment = array(
'author' => 'viagra-test-123',
'email' => 'test@example.com',
'website' => 'http://www.example.com/',
'body' => 'This is a test comment',
'permalink' => 'http://your-domain.com/path-to-your-comment-page',
'user_ip' => 'an-ip-address', // optional, if not in array defaults to $_SERVER['REMOTE_ADDR']
'user_agent' => 'user-agent-string', // optional, if not in array defaults to $_SERVER['HTTP_USER_AGENT']
);
// instantiate an instance of the class
$akismet = new Akismet('http://www.yourdomain.com/', 'YOUR_WORDPRESS_API_KEY', $comment);
// test for errors
if($akismet->errorsExist() { // returns true if any errors exist
if($akismet->isError('AKISMET_INVALID_KEY')) {
// do something
} elseif($akismet->isError('AKISMET_RESPONSE_FAILED')) {
// do something
} elseif($akismet->isError('AKISMET_SERVER_NOT_FOUND')) {
// do something
}
} else {
// No errors, check for spam
if ($akismet->isSpam()) { // returns true if Akismet thinks the comment is spam
// do something with the spam comment
} else {
// do something with the non-spam comment
}
} |
Example 2, submitting a mis-diagnosed comment:
You should incorporate the submitSpam() and submitHam() calls in your applications. Doing so helps Akisment improve its spam checking algorithms.
Код: |
// load array with comment data
$comment = array(
'author' => 'viagra-test-123',
'email' => 'test@example.com',
'website' => 'http://www.example.com/',
'body' => 'This is a test comment',
'permalink' => 'http://your-domain.com/path-to-your-comment-page',
'user_ip' => 'an-ip-address', // optional, if not in array defaults to $_SERVER['REMOTE_ADDR']
'user_agent' => 'user-agent-string', // optional, if not in array defaults to $_SERVER['HTTP_USER_AGENT']
);
// instantiate an instance of the class
$akismet = new Akismet('http://www.yourdomain.com/', 'YOUR_WORDPRESS_API_KEY', $comment);
// submit the ham or spam comment
if (!$akismet->errorsExist()) {
$akismet->submitHam(); // or submitSpam()
} |
Additional methods
These methods, in additon to those shown above, are also available.
Код: |
$akismet->getError($name); // return a specific error message from the errors array
$akismet->getErrors(); // return an array of all errors |
References