【问题标题】:How to integrate Akismet in wordpress/php programmatically如何以编程方式将 Akismet 集成到 wordpress/php 中
【发布时间】:2015-12-26 11:54:09
【问题描述】:

我想使用 Akismet 检查自定义表单以防止垃圾邮件。 我在网上搜索,发现唯一简单的方法是(见下面的代码摘录):http://www.binarymoon.co.uk/2010/03/akismet-plugin-theme-stop-spam-dead/。不幸的是 $isSpam 返回真!

有谁知道如何施展魔法?我感谢您的帮助。

function akismet_isSpam ($content) {

// innocent until proven guilty
$isSpam = FALSE;

$content = (array) $content;

if (function_exists('akismet_init')) {

    $wpcom_api_key = get_option('wordpress_api_key');

    if (!empty($wpcom_api_key)) {

        global $akismet_api_host, $akismet_api_port;

        // set remaining required values for akismet api
        $content['user_ip'] = preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] );
        $content['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
        $content['referrer'] = $_SERVER['HTTP_REFERER'];
        $content['blog'] = get_option('home');

        if (empty($content['referrer'])) {
            $content['referrer'] = get_permalink();
        }

        $queryString = '';

        foreach ($content as $key => $data) {
            if (!empty($data)) {
                $queryString .= $key . '=' . urlencode(stripslashes($data)) . '&';
            }
        }

        $response = akismet_http_post($queryString, $akismet_api_host, '/1.1/comment-check', $akismet_api_port);
        smart_dump($response, true);

        if ($response[1] == 'true') {
            update_option('akismet_spam_count', get_option('akismet_spam_count') + 1);
            $isSpam = TRUE;
        }

    }

}

return $isSpam;

}

【问题讨论】:

    标签: php wordpress spam-prevention akismet


    【解决方案1】:

    首先,您可以假设安装了 akismet 并验证了 AP​​I 密钥,这将允许您直接使用 akismet_http_post 函数将数据发布到服务器..

    // Like above mentioned, We assume that :
    // Akismet is installed with the corresponding API key
    if( function_exists( 'akismet_http_post' ) )
    {   
        global $akismet_api_host, $akismet_api_port;
    
        // data to be delivered to Akismet (This is what you need Modify this to your needs)
        $data = array( 
            'comment_author'        => 'Spammer Spammy',
            'comment_author_email'  => 'spammy@spamserver.com',
            'comment_author_url'    => 'spamming.com',
            'comment_content'       => 'my Spam',
            'user_ip'               => '99.99.99.99',
            'user_agent'            => '',
            'referrer'              => '',
            'blog'                  => 'http://example.com',
            'blog_lang'             => 'en_US',
            'blog_charset'          => 'UTF-8',
            'permalink'             => 'http://example.com/my-link',
            'is_test'               => TRUE,
        );
    
        // Now we need to construct the query string
        $query_string = http_build_query( $data );
        // and then post it to Akismet
        $response = akismet_http_post( $query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port );
        // ..and check the results        
        $result = ( is_array( $response ) && isset( $response[1] ) ) ? $response[1] : 'false';
        // display the result ( 'true', 'false' or any error message you want )    
        printf( 'Spam check according to Akismet :%s', $result );
    }
    

    这没有经过彻底测试,但应该可以工作..

    请注意,akismet_http_post 函数需要 bloguser_ipuser_agent 参数。

    另外,很高兴提到 Akismet 有 libraries 和良好的 API 文档。

    【讨论】:

    • 解决方案有效(返回真)。但是,我不完全确定 Akismet 是否在做它的工作,因为当我通过我在网上找到的测试字符串时它也会返回 true ..(据说 $response 数组似乎已正确指定)。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    相关资源
    最近更新 更多