gothick / php-akismet

Akismet PHP 客户端

1.0.0 2017-09-21 00:00 UTC

This package is not auto-updated.

Last update: 2024-09-29 04:20:18 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Build Status

一个简单的 PHP Akismet 客户端。

  • PSR-4 自动加载
  • Composer 兼容
  • 使用 Guzzle 作为其 HTTP 客户端
  • 公开所有 Akismet 方法及其返回值

简单用法

垃圾邮件检查

使用 Akismet 的 comment-check API 方法

    $client = new \Gothick\AkismetClient\Client(
        "http://example.com",   // Your website's URL (this becomes Akismet's "blog" parameter)
        "Example Forum",        // Your website or app's name (Used in the User-Agent: header when talking to Akismet)
        "1.2.3",                // Your website or app's software version (Used in the User-Agent: header when talking to Akismet)
        "YOUR KEY HERE"         // Your Akismet API key
    );
    
    // See https://akismet.com/development/api/#comment-check for all available parameters
    $params = [
        "user_ip" => "203.0.113.4", // IP address of person posting the comment
        "user_agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8", // User-Agent header of the commenter 
        "comment_type" => "forum-post",
        "comment_author" => "Spammy McSpamface",
        "comment_content" => "I'm an evil spammy message"
    ]
        
    // $result will be of type \Gothick\AkismetClient\Result\CommentCheckResult.php
    // Akismet really wants to see your $_SERVER variables. "This data is highly useful to
    // Akismet. How the submitted content interacts with the server can be very telling,
    // so please include as much of it as possible." But obviously, if you're worried,
    // you could filter anything senstive out and send in a pared-down array instead.
    $result = $client->commentCheck($params, $_SERVER);
    
    $is_spam = $result->isSpam(); // Boolean 

更高级的用法

    // ...get $result from client as above...
    // If it's blatant spam that Akismet thinks you can discard without human
    // intervention (see https://blog.akismet.com/2014/04/23/theres-a-ninja-in-your-akismet/)
    $is_blatant_spam = $result->isBlatantSpam();

    // Get the X-akismet-pro-tip header, if present
    if ($result->hasProTip()) {
        $pro_tip = $result->getProTip();
    }
    
    // Get the X-akismet-debug-help header, if present
    if ($result->hasDebugHelp()) {
        $debug_help = $result->getDebugHelp();
    }

验证您的 API 密钥

    $client = new \Gothick\AkismetClient\Client(
        "http://example.com",   // Your website's URL (this becomes Akismet's "blog" parameter)
        "Example Forum",        // Your website or app's name (Used in the User-Agent: header when talking to Akismet)
        "1.2.3",                // Your website or app's software version (Used in the User-Agent: header when talking to Akismet)
        "YOUR KEY HERE"         // Your Akismet API key
    );

    // $result will be of type \Gothick\AkismetClient\Result\VerifyKeyResult
    $result = $client->verifyKey();
    $api_key_is_valid = $result->isValid(); // Boolean
    
    // Can also check pro tip and debug help as above.

提交垃圾邮件和正常邮件

此客户端还公开了 Akismet 的 submit-spamsubmit-ham 方法。与上面的 commentCheck 一样使用它们,传递完全相同的参数。有关更多详细信息,请参阅 Akismet API 文档。

    $client->submitHam($params, $_SERVER);
    // OR
    $client->submitSpam($params, $_SERVER);

使用自定义 Guzzle 客户端

如果您有特定的网络传输需求,可以通过将 Guzzle 客户端作为最后一个构造函数参数传递来覆盖 Akismet 客户端使用的默认 Guzzle 客户端

$guzzle_client = new \GuzzleHttp\Client([
    'timeout' => 10.0,
    'handler' => $my_special_handler_stack
]);
$akismet_client = new \Gothick\AkismetClient\Client(
    "http://example.com",   // Your website's URL (this becomes Akismet's "blog" parameter)
    "Example Forum",        // Your website or app's name (Used in the User-Agent: header when talking to Akismet)
    "1.2.3",                // Your website or app's software version (Used in the User-Agent: header when talking to Akismet)
    "YOUR KEY HERE",        // Your Akismet API key
    $guzzle_client 
);

错误处理

客户端应该要么正常运行,要么抛出 \Gothick\AkismetClient\AkismetException,这是 PHP \Exception 基类的一个完全平凡的扩展。

测试

提供了一个单元测试套件;使用 Composer 安装具有开发需求的包,然后(您需要 PHP 7.0+)

    php vendor/bin/phpunit -c test/phpunit.xml.dist 

您会注意到一些测试被跳过了。大多数测试使用模拟的 Guzzle 响应,不需要网络连接,并且不接触 Akismet 服务器。如果您希望运行连接到 API 服务器的“实时”测试,请提供一个环境变量中的 API 密钥。

    export AKISMET_API_KEY="YOUR API KEY"
    php vendor/bin/phpunit -c test/phpunit.xml.dist