2015-09-29 54 views
1

我想使用Akismet檢查針對垃圾郵件的自定義表單。 我搜索了網頁,我發現的唯一簡單方法是(見代碼摘錄如下):http://www.binarymoon.co.uk/2010/03/akismet-plugin-theme-stop-spam-dead/。不幸的是,$ isSpam返回true!如何以編程方式將wordpress/php中的Akismet集成

有誰知道如何做魔法?我感謝您的幫助。

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; 

}

回答

1

首先,你可以假設Akismet在安裝和API密鑰驗證,這將允許您直接使用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' => '[email protected]', 
     '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); 
} 

這是沒有徹底測試,但應該工作..

請注意,blog,user_ip和函數需要3210個參數。

此外,很高興提及Akismet有libraries和良好API文件。

+0

該解決方案有效(返回true)。然而,我不完全確定Akismet正在做它的工作,因爲當我傳遞一個我在網上找到的測試字符串時它也會返回true。(說過$ response數組似乎已被正確指定)。謝謝。 – user1232551

相關問題