fyrts / akismet
实现 Akismet 阻止垃圾邮件的 PHP 库
1.1.0
2021-10-26 06:44 UTC
Requires
- php: ^7.1|^8.0
- guzzlehttp/guzzle: ~6.0|~7.0
Requires (Dev)
- phpunit/phpunit: ^8.4|^9.0
README
这是 Akismet API 的 PHP 客户端。Akismet 使用条款适用于此库的使用。
安装
composer require fyrts/akismet
使用
use Akismet\Akismet; use Akismet\Comment; // Instantiate API client with your API key and website root URL $akismet = new Akismet('api-key', 'https://www.example.org'); // Define content to check for spam $comment = new Comment(); $comment->setAuthor('Author Name') ->setAuthorEmail('author@example.org') ->setContent('Lorem ipsum dolor sit amet, consectetur adipiscing elit.'); // Optionally include additional environment variables for more accurate result $comment->includeServerVariables(); // Check for spam $result = $akismet->check($comment); if ($result->isSpam) { echo 'Comment is spam'; } if ($result->isDiscardable) { echo 'Comment is safe to discard'; }
如果需要,可以直接设置 Akismet 参数。参数列表可在官方 API 文档中找到。
$comment = new Comment([ 'comment_author' => 'Author Name', 'comment_author_email' => 'author@example.org', 'comment_content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', ]); $comment->includeServerVariables();
如果您希望手动提供所有参数,可以直接将数组传递给客户端。
$result = $akismet->check([ 'comment_author' => 'Author Name', 'comment_author_email' => 'author@example.org', 'comment_content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', ]);
设置全局参数
对于检查多个评论,可以为所有请求设置全局语言、字符集和环境参数。
$akismet->setLanguage('en'); $akismet->setCharset('UTF-8'); $akismet->includeServerVariables(); $result = $akismet->check($comment);
可用的端点
以下端点可以通过 Akismet\Akismet 类访问
Akismet::verifyKey()
Akismet::check($comment)
或Akismet::commentCheck($comment)
Akismet::submitSpam($comment)
Akismet::submitHam($comment)
可用的评论参数方法
以下方法可以通过 Akismet\Comment 类访问以设置参数
Comment::setUserIp($ip)
Comment::setUserAgent($user_agent)
Comment::setReferrer($referrer)
或Comment::setReferer($referrer)
Comment::setPermalink($permalink)
Comment::setType($type)
或Comment::setCommentType($type)
Comment::setAuthor($author)
或Comment::setCommentAuthor($author)
Comment::setAuthorEmail($email)
或Comment::setCommentAuthorEmail($email)
Comment::setAuthorUrl($url)
或Comment::setCommentAuthorUrl($url)
Comment::setContent($content)
或Comment::setCommentContent($content)
Comment::setDate($date)
或Comment::setCommentDate($date)
Comment::setPostModifiedDate($date)
或Comment::setCommentPostModifiedDate($date)
Comment::setBlogLanguage($language)
Comment::setBlogCharset($charset)
Comment::setUserRole($role)
Comment::setTest()
Comment::setRecheckReason($reason)
可以通过 Comment::setParameter($key, $value)
设置任何自定义参数。
日期参数接受 DateTime 对象、Unix 时间戳或 ISO 8601 格式的日期字符串。
许可证
fyrts/akismet
根据 MIT 许可证 (MIT) 许可。有关更多信息,请参阅LICENSE。