Services_Akismet2
[ class tree: Services_Akismet2 ] [ index: Services_Akismet2 ] [ all elements ]

Class: Services_Akismet2

Source Location: /Services_Akismet2-0.3.1/Services/Akismet2.php

Class Overview


Class to use Akismet API from PHP


Author(s):

Copyright:

  • 2007-2008 Bret Kuhns, 2008 silverorange

Variables

Methods


Inherited Variables

Inherited Methods


Class Details

[line 179]
Class to use Akismet API from PHP

Example usage:

  1.  /*
  2.   * Handling user-posted comments
  3.   */
  4.  
  5.  $comment = new Services_Akismet2_Comment(array(
  6.      'author'      => 'Test Author',
  7.      'authorEmail' => 'test@example.com',
  8.      'authorUri'   => 'http://example.com/',
  9.      'content'     => 'Hello, World!'
  10.  ));
  11.  
  12.  try {
  13.      $apiKey 'AABBCCDDEEFF';
  14.      $akismet = new Services_Akismet2('http://blog.example.com/'$apiKey);
  15.      if ($akismet->isSpam($comment)) {
  16.          // rather than simply ignoring the spam comment, it is recommended
  17.          // to save the comment and mark it as spam in case the comment is a
  18.          // false positive.
  19.      else {
  20.          // save comment as normal comment
  21.      }
  22.  catch (Services_Akismet2_InvalidApiKeyException $keyException{
  23.      echo 'Invalid API key!';
  24.  catch (Services_Akismet2_HttpException $httpException{
  25.      echo 'Error communicating with Akismet API server: ' .
  26.          $httpException->getMessage();
  27.  catch (Services_Akismet2_InvalidCommentException $commentException{
  28.      echo 'Specified comment is missing one or more required fields.' .
  29.          $commentException->getMessage();
  30.  }
  31.  
  32.  /*
  33.   * Submitting a comment as known spam
  34.   */
  35.  
  36.  $comment = new Services_Akismet2_Comment(array(
  37.      'author'      => 'Test Author',
  38.      'authorEmail' => 'test@example.com',
  39.      'authorUri'   => 'http://example.com/',
  40.      'content'     => 'Hello, World!'
  41.  ));
  42.  
  43.  try {
  44.      $apiKey 'AABBCCDDEEFF';
  45.      $akismet = new Services_Akismet2('http://blog.example.com/'$apiKey);
  46.      $akismet->submitSpam($comment);
  47.  catch (Services_Akismet2_InvalidApiKeyException $keyException{
  48.      echo 'Invalid API key!';
  49.  catch (Services_Akismet2_HttpException $httpException{
  50.      echo 'Error communicating with Akismet API server: ' .
  51.          $httpException->getMessage();
  52.  catch (Services_Akismet2_InvalidCommentException $commentException{
  53.      echo 'Specified comment is missing one or more required fields.' .
  54.          $commentException->getMessage();
  55.  }
  56.  
  57.  /*
  58.   * Submitting a comment as a false positive
  59.   */
  60.  
  61.  $comment = new Services_Akismet2_Comment(array(
  62.      'author'      => 'Test Author',
  63.      'authorEmail' => 'test@example.com',
  64.      'authorUri'   => 'http://example.com/',
  65.      'content'     => 'Hello, World!'
  66.  ));
  67.  
  68.  try {
  69.      $apiKey 'AABBCCDDEEFF';
  70.      $akismet = new Services_Akismet2('http://blog.example.com/'$apiKey);
  71.      $akismet->submitFalsePositive($comment);
  72.  catch (Services_Akismet2_InvalidApiKeyException $keyException{
  73.      echo 'Invalid API key!';
  74.  catch (Services_Akismet2_HttpException $httpException{
  75.      echo 'Error communicating with Akismet API server: ' .
  76.          $httpException->getMessage();
  77.  catch (Services_Akismet2_InvalidCommentException $commentException{
  78.      echo 'Specified comment is missing one or more required fields.' .
  79.          $commentException->getMessage();
  80.  }



[ Top ]


Class Variables

$apiKey =  ''

[line 232]

The API key to use to access Akismet services

Type:   string


[ Top ]

$apiKeyIsValid =  null

[line 248]

Whether or not the API key is valid

Type:   boolean


[ Top ]

$apiPort =  80

[line 192]

The port to use to connect to the Akismet API server

Defaults to 80.


Type:   integer


[ Top ]

$apiServer =  'rest.akismet.com'

[line 203]

The Akismet API server name

Defaults to 'rest.akismet.com'.


Type:   string


[ Top ]

$apiVersion =  '1.1'

[line 214]

The Akismet API version to use

Defaults to '1.1'.


Type:   string


[ Top ]

$blogUri =  ''

[line 223]

The URI of the webblog for which Akismet services will be used

Type:   string


[ Top ]

$request =  null

[line 239]


Type:   HTTP_Request2


[ Top ]

$userAgent =  ''

[line 259]

The HTTP user-agent to use

If this is an empty string, a default user-agent string is generated and used.


Type:   mixed


[ Top ]



Method Detail

__construct (Constructor)   [line 278]

Services_Akismet2 __construct( string $blogUri, string $apiKey, [ $config = array()], [ $request = null])

Creates a new Akismet object

Parameters:

string   $blogUri     the URI of the webblog homepage.
string   $apiKey     the API key to use for Akismet services.
array   $config     optional. An associative array of configuration options. See Services_Akismet2::setConfig().
HTTP_Request2   $request     optional. The HTTP request object to use. If not specified, a HTTP request object is created automatically.

[ Top ]

getUserAgent   [line 651]

string getUserAgent( )

Gets the HTTP user-agent used to make Akismet requests

[ Top ]

isApiKeyValid   [line 629]

boolean isApiKeyValid( string $key)

Checks with the Akismet server to determine if an API key is valid
  • Return: true if the key is valid and false if it is not valid.
  • Throws: Services_Akismet2_HttpException if there is an error communicating with the Akismet API server.
  • Access: protected

Parameters:

string   $key     the API key to check.

[ Top ]

isSpam   [line 398]

boolean isSpam( Services_Akismet2_Comment|array $comment, [boolean $autoSetServerFields = false])

Checks whether or not a comment is spam

When checking if a comment is spam, it is possible to set the required fields, and several other server-related fields automatically. When the server-related fields are set automatically, usually only the content-related fields of the comment need to be specified manually.

Note: Only auto-set server-related fields on comments checked in real-time. If you check comments using an external system, you run the risk of submitting your own server information as spam. Instead, save the server-related fields in the database and set them on the comment using Services_Akismet2_Comment::setField().

  • Return: true if the comment is spam and false if it is not.
  • Throws: Services_Akismet2_HttpException if there is an error communicating with the Akismet API server.
  • Throws: InvalidArgumentException if the provided comment is neither an array not an instanceof Services_Akismet2_Comment.
  • Throws: Services_Akismet2_InvalidCommentException if the specified comment is missing required fields.
  • Throws: Services_Akismet2_InvalidApiKeyException if the provided API key is not valid.
  • Access: public

Parameters:

Services_Akismet2_Comment|array   $comment     the comment to check.
boolean   $autoSetServerFields     whether or not to automatically set server-related fields. Defaults to false.

[ Top ]

sendRequest   [line 548]

string sendRequest( string $methodName, [ $params = array()], [string $apiKey = ''])

Calls a method on the Akismet API server using a HTTP POST request
  • Return: the HTTP response content.
  • Throws: Services_Akismet2_HttpException if there is an error communicating with the Akismet API server.
  • Access: protected

Parameters:

string   $methodName     the name of the Akismet method to call.
array   $params     optional. Array of request parameters for the Akismet call.
string   $apiKey     optional. The API key to use. Not required if verifying an API key. Required for all other request types.

[ Top ]

setConfig   [line 334]

Services_Akismet2 setConfig( string|array $name, [string|null $value = null])

Sets one or more configuration values

Configuration values are:

  • apiServer - the API server to use. By default, the Akismet API server (owned by Wordpress.com) is used. Set this to use an alternate Akismet API service provider.
  • apiPort - the HTTP port to use on the API server.
  • apiVersion - the API version to use.
  • userAgent - the HTTP user-agent to use. By default, the user-agent @name@/@api-version@ | Akismet/1.1 is used.
Example usage:
  1.  // sets config using an associative array
  2.  $akismet->setConfig(array(
  3.      'apiServer' => 'rest.akismet.com',
  4.      'apiPort'   => 80
  5.  ));
  6.  
  7.  // sets config using fluent interface
  8.  $akismet->setConfig('apiServer''rest.akismet.com')
  9.          ->setConfig('apiPort'80);

  • Return: the Akismet API object.
  • Access: public

Parameters:

string|array   $name     config name or an associative array containing configuration name-value pairs.
string|null   $value     config value. Ignored if $name is an array.

[ Top ]

setRequest   [line 523]

Services_Akismet2 setRequest( $request)

Sets the HTTP request object to use
  • Return: the Akismet API object.
  • Access: public

Parameters:

HTTP_Request2   $request     the HTTP request object to use.

[ Top ]

submitFalsePositive   [line 492]

Services_Akismet2 submitFalsePositive( Services_Akismet2_Comment|array $comment)

Submits a false-positive comment to the Akismet server

Use this method to submit comments that are detected as spam but are not actually spam.

  • Return: the Akismet API object.
  • Throws: Services_Akismet2_HttpException if there is an error communicating with the Akismet API server.
  • Throws: InvalidArgumentException if the provided comment is neither an array not an instanceof Services_Akismet2_Comment.
  • Throws: Services_Akismet2_InvalidCommentException if the specified comment is missing required fields.
  • Throws: Services_Akismet2_InvalidApiKeyException if the provided API key is not valid.
  • Access: public

Parameters:

Services_Akismet2_Comment|array   $comment     the comment that is <em>not</em> spam.

[ Top ]

submitSpam   [line 445]

Services_Akismet2 submitSpam( Services_Akismet2_Comment|array $comment)

Submits a comment as an unchecked spam to the Akismet server

Use this method to submit comments that are spam but are not detected by Akismet.

  • Return: the Akismet API object.
  • Throws: Services_Akismet2_HttpException if there is an error communicating with the Akismet API server.
  • Throws: InvalidArgumentException if the provided comment is neither an array not an instanceof Services_Akismet2_Comment.
  • Throws: Services_Akismet2_InvalidCommentException if the specified comment is missing required fields.
  • Throws: Services_Akismet2_InvalidApiKeyException if the provided API key is not valid.
  • Access: public

Parameters:

Services_Akismet2_Comment|array   $comment     the comment to submit as spam.

[ Top ]

validateApiKey   [line 600]

void validateApiKey( )

Validates the API key before performing a request
  • Throws: Services_Akismet2_InvalidApiKeyException if the provided API key is not valid.
  • Access: protected

[ Top ]


Documentation generated on Mon, 25 Oct 2010 14:00:24 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.