Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/gsteel/akismet

Straight-forward PHP API Client for the Akismet service
https://github.com/gsteel/akismet

akismet akismet-api akismet-client php

Last synced: about 1 month ago
JSON representation

Straight-forward PHP API Client for the Akismet service

Awesome Lists containing this project

README

        

# Akismet Client Library

[![Continuous Integration](https://github.com/gsteel/akismet/actions/workflows/continuous-integration.yml/badge.svg)](https://github.com/gsteel/akismet/actions/workflows/continuous-integration.yml)
[![Psalm Type Coverage](https://shepherd.dev/github/gsteel/akismet/coverage.svg)](https://shepherd.dev/github/gsteel/akismet)
[![Latest Stable Version](https://poser.pugx.org/gsteel/akismet/v/stable)](https://packagist.org/packages/gsteel/akismet)
[![Total Downloads](https://poser.pugx.org/gsteel/akismet/downloads)](https://packagist.org/packages/gsteel/akismet)

## Introduction

Provides a straight-forward way of using the [Akismet](https://akismet.com) anti-spam service in any-old PHP application.

## Installation & Requirements

Requires PHP `~8.0 || ~8.1 || ~8.2`

The library does not include an HTTP client, so if your project does not already have a [PSR-18 HTTP Client](https://www.php-fig.org/psr/psr-18/) installed, you will need to install one in order to use it. There are many [HTTP clients to choose](https://packagist.org/providers/psr/http-client-implementation) from, for example the popular libraries [Guzzle](https://packagist.org/packages/guzzlehttp/guzzle) or [HttpPlug](https://packagist.org/packages/php-http/curl-client).

The library also requires that you have a [PSR-17 (HTTP Factories) library](https://www.php-fig.org/psr/psr-17/) installed, again, you can find implementations on [Packagist](https://packagist.org/providers/psr/http-factory-implementation) and I personally favour [laminas/laminas-diactoros](https://packagist.org/packages/laminas/laminas-diactoros).

```shell
composer require laminas/laminas-diactoros
composer require php-http/curl-client
composer require gsteel/akismet
```

## Basic Usage

### Construct a client

Once you have the requisite HTTP related libraries installed, they _should_ become discoverable with the shipped [discovery library](https://github.com/php-http/discovery). Provide an Api key, and the default website address that you will be operating with to the constructor, and you’ll have a ready-to-use client:

```php
verifyKey();
assert($result === true);

```

### Comment Parameters & Checking Content

The primary concern of the library is to check requests such as comments and form submissions to ascertain whether the content is "Spam" or "Ham".

The Akismet API has a number of parameters available to improve the accuracy of the check which have been encapsulated into an immutable value object `\Gsteel\Akismet\CommentParameters`.

If you are familiar with the parameter names, you can pass in an array to the constructor of this object, otherwise you can call various methods to build an object to include all the information you wish to provide.

```php
withComment('Some comment Content', CommentType::contactForm())
->withRequestParams($_SERVER['REMOTE_ADDR']);

$result = $client->check($parameters);

assert($result->isSpam());

```

There are a considerable number of additional arguments and methods available that can be used to provide as much context as possible to the API, there is also a named constructor that is useful in an environment that utilises [PSR-7 Messages](https://www.php-fig.org/psr/psr-7/):

```php
check($parameters);

// Result is incorrectly classified as spam:
$client->submitHam($result->parameters());

// Result is incorrectly classified as ham:
$client->submitSpam($result->parameters());

```

You can serialise and un-serialise a `Result` from the clients `check()` method to a JSON string:

```php
[
'key' => 'Your API Key',
'website' => 'https://you.example.com',
],
];
```

## License

Released under the MIT License - see the [LICENSE](./LICENSE) file for details