Fuspam Akismet Function
Summary

Akismet is a service designed to stop comment spam on blogs. It is built-in to WordPress, however it also has a public API you can use to build Akismet support into any app you create. It's highly effective at stopping comment spam, web contact form spam, and any other interface where users submit data to a website. Spammers have no shame, they will try to spam anything.

Fuspam is our own very simple PHP function that makes using Akismet in your php application very simple. Fuspam relies on the unix program `curl`, so make sure you have that installed before using our script.

akismet.fuspam.php 1.1 (2 KB) 2/6/2010
akismet.fuspam.php 1.0 (2 KB) 12/14/2009

The Code

<?
// Fuspam 1.1
// F-U-Spam!
// This is the fully-commented version of the script
// The downloadable version is much leaner
// This script uses/requires the `curl` CLI program
// http://www.whatsmyip.org/fuspam-akismet-php/

// How to use this script: First include the script.
Set the 'blog' variable if you are verifying your key, otherwise set ALL variables.
Then call the fuspam( ) function and use the results accordingly


include("akismet.fuspam.php");

$comment['blog'] = "";
// The front page or home URL of the instance making the request (your blog/webapp etc). Note: Must be a full URI, including http://

$comment['user_ip'] = "";
// IP address of the comment submitter

$comment['user_agent'] = "";
// User Agent of commenter/spammer (not YOUR user agent!)

$comment['referrer'] = "";
// The content of the HTTP_REFERER header

$comment['permalink'] = "";
// The permanent location of the entry the comment was submitted to.

$comment['comment_type'] = "";
// May be blank, comment, trackback, pingback, or a made up value like "registration", "email", "review" etc.

$comment['comment_author'] = "";
// Submitted name with the comment

$comment['comment_author_email'] = "";
// Submitted email address

$comment['comment_author_url'] = "";
// Commenter's URL.

$comment['comment_content'] = "";
// The content that was submitted

// When submitting data back to Akismet, you have to include all of this data. In other words, you have to store the commenter/spammer's IP addresses, User Agents, and Referers in your comment database. Don't submit spam/ham with your own User Agent etc!

// Once you fill up the $comment array, you simply call the fuspam( ) function. It's input is as follows:
// $comment is the array with all of the comment data in it
// $type is a string with the following possible values:
// "check-spam" - used for seeing if a comment is spam
// "submit-spam" - used for submitting a comment to Akismet as spam (when misdiagnosed as not-spam)
// "submit-ham" - used for submitting a comment that is NOT spam, back to Akistmet (when misdiagnosed as spam)
// "verify-key" - used for verifying your akismet key
// $key is your akismet key. You have to get a key before you can use this service. Go to akismet.com


function fuspam( $comment , $type , $key )
    {
    $comment = array_map("urlencode",$comment);
    // encodes all of the data in each row of the $comment function
    
     $post = "blog={$comment['blog']}&";
    $post .= "user_ip={$comment['user_ip']}&";
    $post .= "user_agent={$comment['user_agent']}&";
    $post .= "referrer={$comment['referrer']}&";
    $post .= "permalink={$comment['permalink']}&";
    $post .= "comment_type={$comment['comment_type']}&";
    $post .= "comment_author={$comment['comment_author']}&";
    $post .= "comment_author_email={$comment['comment_author_email']}&";
    $post .= "comment_author_url={$comment['comment_author_url']}&";
    $post .= "comment_content={$comment['comment_content']}";
    // build the body of the POST request. This sends all of the comment data to akismet
    
    switch ($type)
        {
       case "verify-key":
            $call = "/1.1/verify-key";
            $post = "key={$key}&blog={$comment['blog']}";
            break;
            // if you are verifying your key, use the verify key url
            
       case "check-spam":
            $call = "/1.1/comment-check";
            break;
            // if you are checking if a comment is spam, use the spam checking url
            
        case "submit-spam":
            $call = "/1.1/submit-spam";
            break;
            // if you are submitting spam, use the spam submission url
            
        case "submit-ham":
            $call = "/1.1/submit-ham";
            break;
            // if you are submitting a non-spam, use the ham submission url
            
        default:
            return "Error: 'type' not recognized";
            break;
            // if the type you pass to fuspam( ) isn't recognized, return an error
        }
    
    $command = "curl -A \"Fuspam/1.1 | Akismet/1.11\" -m 5 -d \"$post\" http://$key.rest.akismet.com$call";
    // build the command we are going to run. This is where the magic happens, this uses curl to send all our data to akismet
    
    $i = 0;
    do
        {
        // this loop tries to contact the akismet server up to 5 times before giving up. Very helpful in overcoming network instability
        
        $result = exec($command);
        // run the command
        
        if (preg_match("/^curl: \(([0-9]+)\)/",$result))
            { $success = false; }
        else
            { $success = true; }
        // check to see if curl returned an error, or not
        
        $i++;
        }
    while ( ($i < 6) and ($success == false) );

    return $result;
    // The end. The script is done, return the result - see below
    }

// fuspam( ) always returns a string. Here are it's possible return values:
// "true" - returned if it finds a comment to be a spam
// "false" - returned if it finds a comment to be legit
// "valid" - returned if your akismet key is valid (when verifying)
// "invalid" - returned if your akismet key is not valid
// "Error: 'type' not recognized" - returned when the $type you pass to fuspam( ) is not valid
// When the script can't contact the Akismet server, it will return an error from the `curl` program.
// When you successfully submit spam or ham to akismet, the function will return a thank you message that is the akismet server's response

?>
Usage Tips

For questions and discussion about this script, check out the Forum

When submitting data to akismet, make sure you are setting all of the proper values in your array. This means you may have to add a few columns to your database to store data like the poster's User Agent, etc. You want to submit all the info. The more accurate akismet is, the better for you and all it's other users.

The User Agent string is too short to effectively compress, so I wouldn't bother. However you can use the MySQL functions INET_ATON( ) and INET_NTOA( ) to store IP addresses as integers instead of strings. That compresses them quite a bit and also makes working with them faster.

There are many ways to handle the results of fuspam( ). You have to think about what procedure you want. "true" messages are probably spam, and "false" messages probably are not. If there is a server outage, or some other kind of error, you need to decide what to do with the data. You can assume on error, that the comment is not spam. Or you could do the opposite and assume it is spam. If you have a flag-based spam management system, you could submit a flag value half as strong as a "true", when the function returns an error.

©2000-2010 / Contact Us / facebook / Forum