Last Modified: December 21, 2008 (17:14), Keywords: PHP, ZendFramework
Zend_Service_Mollom is a Zend Framework component for calling Mollom, a spam filtering and CAPTCHA service for blog comments and forum posts. This page serves as the project homepage, with download information and documentation.
Mollom, like Akismet filters your user-submitted content. But with an approach that should work better. Mollom integrates automatic checking with CAPTCHAs. More info is available on the How Mollom Works page.
Zend Framework is a framework for building PHP applications. The Zend_Service_Mollom component integrates the two. Zend_Service_Mollom implements the full Mollom API and is tested using unit tests.
The latest version for Zend_Service_Mollom is v1.2.0. There are two ways to get it:
git clone git://git.savanne.be/git/zend.service.mollom.gitChanges compared to v1.1.0:
A full changelog is available in the README.txt file. I am proposing this version for inclusion into Zend Framework 1.8. For now however, you will have to get your releases here.
Zend_Service_Mollom was written by Ruben Vermeersch. All questions, remarks & feedback are welcome, I am also available for contracting on PHP related projects.
Assuming you have installed your Zend Framework application correctly, as well as the Zend_Service_Mollom files (install instructions can be found in INSTALL.txt), using Zend_Service_Mollom is just like any other zend Framework component:
Zend_Service_Mollom object- // Keys can be obtained in the Mollom site manager.
- $public = 'your-public-key';
- $private = 'your-private-key';
- $mollom = new Zend_Service_Mollom($public, $private);
This object can then be used to check your user-submitted content:
- $data = array('post_title' => $_POST['comment_title'],
- 'post_body' => $_POST['comment_body'],
- 'author_name' => $_POST['author_name']);
- $results = $mollom->checkContent($data);
-
- /* $results is now an array in the form of:
- *
- * array(3) {
- * ["quality"]=>
- * float(0)
- * ["session_id"]=>
- * string(16) "aab0a316d1770d73"
- * ["classification"]=>
- * string(4) "spam"
- * }
- */
As you can see, Mollom provides you with a quality ranking (zero in this case), a session id and a classification (which can be spam, unsure or ham). The method uses an array parameter which can be used to supply as much info as you can. The more you supply, the better Mollom works. You can use the following fields:
session_idpost_titlepost_bodyauthor_nameauthor_urlauthor_mailauthor_openidauthor_ipauthor_idA full description of these fields is in the Mollom API documentation.
In the case of an unsure match, you should show a CAPTCHA to the user. This can be requested using the getAudioCaptcha or getImageCaptcha methods:
- $captcha = $mollom->getImageCaptcha($session_id, $author_id);
-
- /* $captcha is now an array in the form of:
- *
- * array(2) {
- * ["session_id"]=>
- * string(16) "f5fe699abf7b9512"
- * ["url"]=>
- * string(49) "http://xmlrpc1.mollom.com:80/9b5cfb6f0f9d6af6.png"
- * }
- */
Once solved, this CAPTCHA can be verified using the checkCaptcha method:
- $is_correct = $mollom->checkCaptcha($session_id, $solution);
There are plenty of different ways to use this component. Check the comments in the PHP file (sorry, no API doc yet) and the Mollom API documentation for more information on how to use it. Should be fairly easy.
Optionally, you can supply Zend_Service_Mollom with a cache object, which speeds up the calls made to the Mollom service. Doing this is straightforward:
- $cache = ... // Check the Zend_Cache manual to make a
- // Zend_Cache_Core object
- Zend_Service_Mollom::setCache($cache);
For convenience there are also the static methods getCache(), hasCache(), clearCache() and removeCache().
In future releases, some of the following might appear:
There already is a Mollom library for PHP: PHP Mollom. However, I prefer a Zend Framework version for two reasons: