|
1 day ago | |
---|---|---|
.github | 1 week ago | |
.vscode | 1 month ago | |
doc | 4 days ago | |
etc | 3 days ago | |
example | 1 month ago | |
lib | 3 days ago | |
share | 8 months ago | |
test | 1 month ago | |
var | 3 years ago | |
.editorconfig | 1 year ago | |
.gitattributes | 5 months ago | |
.gitignore | 6 months ago | |
.semver | 7 months ago | |
AUTHORS.txt | 3 years ago | |
CHANGELOG.md | 1 month ago | |
LICENSE.md | 4 days ago | |
README.md | 2 months ago | |
RoboFile.php | 3 days ago | |
composer.json | 1 day ago |
Prevent comment spam using the Akismet connector for Yii, high-performance PHP framework.
The latest PHP and Composer versions to use the Akismet library.
If you plan to play with the sources, you will also need the latest Robo and Material for MkDocs versions.
From a command prompt, run:
composer require cedx/yii2-akismet
In your application configuration file, you can use the following component:
<?php return [
'components' => [
'akismet' => [
'class' => 'yii\akismet\http\Client',
'apiKey' => '123YourAPIKey',
'blog' => [
'class' => 'yii\akismet\Blog',
'url' => 'http://www.yourblog.com'
]
]
]
];
Once the yii\akismet\http\Client
component initialized with your credentials, you can use its methods.
<?php
use yii\akismet\http\{ClientException};
try {
$client = \Yii::$app->akismet;
$isValid = $client->verifyKey();
echo $isValid ? 'The API key is valid' : 'The API key is invalid';
}
catch (ClientException $e) {
echo 'An error occurred: ', $e->getMessage();
}
<?php
use yii\akismet\{Author, Comment};
try {
$comment = new Comment(
new Author('127.0.0.1', 'Mozilla/5.0'),
['content' => 'A user comment', 'date' => new \DateTime]
);
$isSpam = $client->checkComment($comment);
echo $isSpam ? 'The comment is spam' : 'The comment is ham';
}
catch (ClientException $e) {
echo 'An error occurred: ', $e->getMessage();
}
<?php
try {
$client->submitSpam($comment);
echo 'Spam submitted';
$client->submitHam($comment);
echo 'Ham submitted';
}
catch (ClientException $e) {
echo 'An error occurred: ', $e->getMessage();
}
The yii\akismet\http\Client
class triggers some events during its life cycle.
Client::eventRequest
eventEmitted every time a request is made to the remote service:
<?php
use yii\akismet\http\{Client};
use yii\httpclient\{RequestEvent};
$client->on(Client::eventRequest, function(RequestEvent $event) {
echo 'Client request: ', $event->request->url;
});
Client::eventResponse
eventEmitted every time a response is received from the remote service:
<?php
use yii\akismet\http\{Client};
use yii\httpclient\{RequestEvent};
$client->on(Client::eventResponse, function(RequestEvent $event) {
echo 'Server response: ', $event->response->statusCode;
});
In order to run the tests, you must set the AKISMET_API_KEY
environment variable to the value of your Akismet API key:
export AKISMET_API_KEY="<123YourAPIKey>"
Then, you can run the test
script from the command prompt:
composer run-script test
Akismet for Yii is distributed under the MIT License.