PHP Classes

How to Implement a Chess Game with AI PHP Script to Automate Moves Using the Package Chess API: Get chess moves from the chess-api.com Web service

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2026-03-11 (1 hour ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
chess-api 1.0MIT/X Consortium ...5Games, Web services, PHP 7
Description 

Author

This package can get chess moves from the chess-api.com web service.

It can send HTTP requests to the chess-api.com web service servers to pass the positions of chess game pieces using the FEN (Forsyth-Edwards Notation) format.

The package can retrieve the API response to obtain suggestions for the next move of the game pieces.

Picture of Massimiliano Arione
  Performance   Level  
Name: Massimiliano Arione <contact>
Classes: 3 packages by
Country: Italy Italy
Age: 51
All time rank: 4419173 in Italy Italy
Week rank: 195 Up8 in Italy Italy Up
Innovation award
Innovation award
Nominee: 1x

Instructions

Please read this document to learn how to get chess game piece move suggestions using PHP.

Documentation

Chess API Client

A PHP client for chess-api.com using PSR-18 HTTP Client.

Requirements

  • PHP 8.2+
  • PSR-18 HTTP Client implementation (e.g. Symfony HttpClient, Guzzle)
  • PSR-17 HTTP Factory implementation

Installation

composer require p-chess/api

If you want to use Symfony HttpClient:

composer require nyholm/psr7 symfony/http-client

If you want to use Guzzle:

composer require guzzlehttp/guzzle

Usage

use PChess\Api\ChessApiClient;

// Create the client with your PSR-18/PSR-17 implementations
$client = new ChessApiClient(
    $httpClient,        // PSR-18 ClientInterface
    $requestFactory,    // PSR-17 RequestFactoryInterface
    $streamFactory,     // PSR-17 StreamFactoryInterface
);

// Get the best move for a position
$fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
$move = $client->getBestMove($fen);
// Returns: 'e4' (or similar)

// Restrict the engine to specific moves
$move = $client->getBestMove($fen, ['e2e4', 'd2d4', 'g1f3']);
// Returns one of the allowed moves

Parameters

| Parameter | Type | Description | |-----------------|----------|--------------------------------------------------------------------------| | $fen | string | The chess position in FEN notation (includes side to move) | | $allowedMoves | array | Optional array of allowed moves in UCI format (e.g., ['e2e4', 'd2d4']) |

Return Value

The method returns a string containing the best move in UCI format (e.g., 'e2e4').

Exceptions

The client throws ChessApiException in the following cases:

  • HTTP error (non-2xx status code)
  • Invalid API response (missing `move` field)
  • API error (e.g., invalid FEN)
use PChess\Api\ChessApiException;

try {
    $move = $client->getBestMove($fen);
} catch (ChessApiException $e) {
    // Handle the error
    echo $e->getMessage();
}

Example with Symfony HttpClient

use Nyholm\Psr7\Factory\Psr17Factory;
use PChess\Api\ChessApiClient;
use Symfony\Component\HttpClient\Psr18Client;

$factory = new Psr17Factory();
$httpClient = new Psr18Client();

$chessClient = new ChessApiClient($httpClient, $factory, $factory);

$fen = 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1';
$move = $chessClient->getBestMove($fen);

echo "Best move: $move\n";

Example with Guzzle

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;
use PChess\Api\ChessApiClient;

$httpClient = new Client();
$factory = new HttpFactory();

$chessClient = new ChessApiClient($httpClient, $factory, $factory);

$fen = 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1';
$move = $chessClient->getBestMove($fen);

echo "Best move: $move\n";

Development

Before exdecuting one of the following, you must execute at least once

docker build -t chess-api .

Running Tests

docker run --rm -v "$(pwd)":/srv/chess-api chess-api vendor/bin/phpunit --color=always

Coding Standard

docker run --rm -v "$(pwd)":/srv/chess-api chess-api vendor/bin/php-cs-fixer --ansi

Static Analysis

docker run --rm -v "$(pwd)":/srv/chess-api chess-api vendor/bin/phpstan --ansi

  Files folder image Files (12)  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (2 files)
Files folder imagetests (1 file)
Accessible without login Plain text file .php-cs-fixer.php Example Example script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file Dockerfile Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file LICENSE.md Lic. License text
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (12)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (12)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file build.yaml Data Auxiliary data

  Files folder image Files (12)  /  src  
File Role Description
  Plain text file ChessApiClient.php Class Class source
  Plain text file ChessApiException.php Class Class source

  Files folder image Files (12)  /  tests  
File Role Description
  Plain text file ChessApiClientTest.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0