Looooong time since I’ve written anything here – about 9 months! This time around I’ve put together a simple PHP5 class for communicating with the Defensio web service. Defensio has an example PHP class, but unfortunately it is PHP4 only and it’s more fun to rewrite it, right?
For those who don’t know about Defensio, it is a web service for determining whether a comment on a blog or message in an application is spam. To use it, you must first register for an API key. Defensio is very similar to the venerable Akismet (see the article I wrote on Akismet) but they provide a couple other features such an indication of the ’spaminess’ of an individual message. Akismet has worked out great for me (123,419 spam comments caught so far!), but I figured it would be good to try out something new as well.
The class is a PHP5 class and the public methods are reasonably well commented. The bottom of this post has a link to a test application and a download of the class/test application. So…the usage of this class is quite straightforward, as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | require('library/Gregphoto/Defensio.php'); require('library/Gregphoto/Defensio/Adapter/Streams.php); $apiKey = 'mysecretapikey'; $siteUrl = 'http://my.secret.site.url.com'; $defensio = new Gregphoto_Defensio($apiKey,$siteUrl); $defensio->setHttpClient(new Gregphoto_Defensio_Adapter_Streams()); $params = array( 'user-ip' => $_SERVER['REMOTE_ADDR'], 'article-date' => '2007/11/24', 'comment-author' => 'Big Bad Spammer', 'comment-type' => 'comment', 'comment-content' => 'please click links and buy viagra', 'comment-author-email' => 'bigbadspammer@annoying.com', 'comment-author-url' => 'http://www.annoying.com' ); $result = $defensio->audit_comment($params); if($result['spam'] == 'true') { echo "Comment is spam with a spam score of " . $result['spaminess']; } else { echo "Comment is not spam"; } |
A couple key points on usage:
- Each Gregphoto_Defensio object requires an Http adapter that it will use to make Http POST requests to the Defensio web service. I originally hardcoded it to use the Zend_Http_Client from the Zend Framework, but figured this could discourage people from using it. Then I wrote up a quick adapter to use PHP’s Http Stream Wrappers, but realized that didn’t work on my Dreamhost account, so I added in an extra one for the Curl extension. In theory I should have added a Gregphoto_Defensio_Adapter_Interface class, but I was lazy and didn’t want to add in an extra file.
- Each Gregphoto_Defensio object can be used to make many requests
- All of the current Defensio API’s are covered by the class. These are covered by the following methods in the Gregphoto_Defensio class:
- validate_key
- announce_article
- audit_comment
- report_false_negatives
- report_false_positives
- get_stats
- Each of these methods takes a single parameter, an associative array of options as defined by the Defensio API. Each API also returns an associative array of response parameters. Two static utility methods Gregphoto_Defensio::getActions (returns a list of defensio methods such as ‘validate-key’) and Gregphoto_Defensio::getActionDetails (returns a list of required parameters, optional parameters, and response parameters for a specific Defensio action) are provided in order to get the details of the input and output parameters.
I created a simple test application which can be used to test Gregphoto_Defensio and all of the APIs provided by Defensio.
Download the Gregphoto_Defensio class and the test application
March 22nd, 2009 at 7:32 am
Are you getting any errors ? what is it or is it not doing ? do you have the same problem in other browsers ?
For the demo, I’ve actually commented out all references to console for the moment, so it should not make any difference to whether the script’s functioning or not.
Also, are you referring to the online demo, or the demo included in the zip ?
December 25th, 2009 at 8:49 am
Excellent material! I was convinced in practice that all works correctly – all it is made simply. If you have time please write the post on blog PHP Form.
Thanks.