PHP code example of tomgould / puzzlerphpsdk

1. Go to this page and download the library: Download tomgould/puzzlerphpsdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tomgould / puzzlerphpsdk example snippets




omGould\PuzzlerPHPSDK\PuzzlerClient;
use TomGould\PuzzlerPHPSDK\Exception\PuzzlerException;

// Initialize the client
$client = new PuzzlerClient(
    'YOUR_CLIENT_ID',
    'YOUR_API_KEY',
    'YOUR_SECRET_KEY'
);

// Check API health
try {
    $health = $client->health()->check();
    echo $health; // the API's health message
} catch (PuzzlerException $e) {
    echo "Error: " . $e->getMessage();
}

// Get all puzzles
try {
    $puzzles = $client->puzzle()->collect();
    print_r($puzzles);
} catch (PuzzlerException $e) {
    echo "Error: " . $e->getMessage();
}

use TomGould\PuzzlerPHPSDK\PuzzlerClient;

$client = new PuzzlerClient(
    'YOUR_CLIENT_ID',      // X-Client-Id
    'YOUR_API_KEY',        // X-Api-Key
    'YOUR_SECRET_KEY',     // Secret Key
    'https://rest-api.puzzlerdigital.uk' // Optional: Base URL
);

$health = $client->health()->check();

$dictionary = $client->puzzle()->dictionary();

// Available puzzle types
print_r($dictionary['types']); // ['XW', 'SU', 'WS', ...]

// Available puzzle names by type
print_r($dictionary['names']); // ['SU' => ['Sudoku'], ...]

[
    'types' => ['XW', 'XC', 'WW', 'WS', 'SW', 'SU', 'JS', 'HM', 'AU'],
    'names' => [
        'XW' => ['Crossword'],
        'XC' => ['Cryptic Crossword'],
        'WW' => ['Word Wheel'],
        'WS' => ['Wordsearch'],
        'SW' => ['Splitwords'],
        'SU' => ['Hard Sudoku', 'Medium Sudoku', 'Easy Sudoku'],
        'JS' => ['Jigsaw'],
        'HM' => ['Hangman'],
        'AU' => ['Add Up']
    ]
]

$puzzles = $client->puzzle()->collect();

$puzzles = $client->puzzle()->collect([
    'puzzleDate' => '2025-11-12'
]);

$puzzles = $client->puzzle()->collect([
    'puzzleDateFrom' => '2025-11-01',
    'puzzleDateTo' => '2025-11-30'
]);

$puzzles = $client->puzzle()->collect([
    'puzzleTypes' => ['XW', 'WS', 'SU']
]);

// Get only "Easy Sudoku" puzzles (DO NOT    'puzzleNames' => ['Easy Sudoku']
]);

// Get all Crosswords and Wordsearches, plus only "Easy Sudoku"
$puzzles = $client->puzzle()->collect([
    'puzzleTypes' => ['XW', 'WS'],  // Note: 'SU' is NOT 

$puzzles = $client->puzzle()->collect([
    'puzzleDate' => '2025-11-12',
    'puzzleTypes' => ['XW', 'WS'],
    'puzzleNames' => ['Easy Sudoku']
]);

$puzzles = $client->puzzle()->collect([
    'puzzleDate' => date('Y-m-d'),
    'puzzleTypes' => ['WS']
]);

$puzzles = $client->puzzle()->collect([
    'puzzleDateFrom' => '2025-11-01',
    'puzzleDateTo' => '2025-11-07',
    'puzzleTypes' => ['SU']
]);

// Get all puzzles from last 7 days
$startDate = date('Y-m-d', strtotime('-7 days'));
$endDate = date('Y-m-d');

$puzzles = $client->puzzle()->collect([
    'puzzleDateFrom' => $startDate,
    'puzzleDateTo' => $endDate
]);

foreach ($puzzles as $puzzle) {
    echo "{$puzzle['name']} - {$puzzle['rdate']}\n";
}

use TomGould\PuzzlerPHPSDK\Exception\AuthenticationException;
use TomGould\PuzzlerPHPSDK\Exception\BadRequestException;
use TomGould\PuzzlerPHPSDK\Exception\NotFoundException;
use TomGould\PuzzlerPHPSDK\Exception\MethodNotAllowedException;
use TomGould\PuzzlerPHPSDK\Exception\ServerException;
use TomGould\PuzzlerPHPSDK\Exception\PuzzlerException;

try {
    $puzzles = $client->puzzle()->collect(['puzzleDate' => '2025-11-12']);
} catch (AuthenticationException $e) {
    // Handle authentication errors (401)
    echo "Authentication failed: " . $e->getMessage();
} catch (BadRequestException $e) {
    // Handle bad request errors (400)
    echo "Bad request: " . $e->getMessage();
    echo "Response: " . $e->getResponseBody();
} catch (NotFoundException $e) {
    // Handle not found errors (404)
    echo "Not found: " . $e->getMessage();
} catch (ServerException $e) {
    // Handle server errors (500, 502)
    echo "Server error: " . $e->getMessage();
} catch (PuzzlerException $e) {
    // Handle any other errors
    echo "Error: " . $e->getMessage();

    // Get raw response body if needed
    if ($e->getResponseBody()) {
        echo "Response: " . $e->getResponseBody();
    }
}

public function __construct(
    string $clientId,
    string $apiKey,
    string $secretKey,
    string $baseUrl = 'https://rest-api.puzzlerdigital.uk'
)

// First, check what's available by getting all puzzles
$allPuzzles = $client->puzzle()->collect();

// Check the date range in the bundle
$dates = array_unique(array_column($allPuzzles, 'rdate'));
print_r($dates);

// Then filter by a date you know exists
$puzzles = $client->puzzle()->collect([
    'puzzleDate' => '2025-11-12'  // Use a date from the above list
]);

// Example: Daily archival script
$client = new PuzzlerClient($clientId, $apiKey, $secretKey);

// Get today's puzzles
$todaysPuzzles = $client->puzzle()->collect([
    'puzzleDate' => date('Y-m-d')
]);

// Store them in your database
foreach ($todaysPuzzles as $puzzle) {
    // Save to database
    $db->insert('puzzle_archive', [
        'puzzle_id' => $puzzle['puzzle_id'],
        'pml_id' => $puzzle['pml_id'],
        'type' => $puzzle['abbr'],
        'name' => $puzzle['name'],
        'date' => $puzzle['rdate'],
        'game_data' => json_encode($puzzle['game_data']),
        'archived_at' => date('Y-m-d H:i:s')
    ]);
}

use TomGould\PuzzlerPHPSDK\PuzzlerClient;
use TomGould\PuzzlerPHPSDK\Exception\AuthenticationException;

// Verify your credentials are correct
$client = new PuzzlerClient(
    'YOUR_CLIENT_ID',
    'YOUR_API_KEY',
    'YOUR_SECRET_KEY'
);

// Test with health check first (public endpoint)
try {
    $health = $client->health()->check();
    echo "Connection successful!\n";
} catch (AuthenticationException $e) {
    echo "Auth failed. Check your credentials.\n";
}

// Check server time
echo "Server time: " . date('Y-m-d H:i:s') . " UTC: " . gmdate('Y-m-d H:i:s') . "\n";

// This will return ALL Sudoku puzzles, ignoring the name filter
$puzzles = $client->puzzle()->collect([
    'puzzleTypes' => ['SU'],  // Do not 

// This will return ONLY "Easy Sudoku" puzzles
$puzzles = $client->puzzle()->collect([
    'puzzleNames' => ['Easy Sudoku']  // Omit 'SU' from puzzleTypes
]);

// Or combine with other types:
$puzzles = $client->puzzle()->collect([
    'puzzleTypes' => ['XW', 'WS'],  // 'SU' is not 

// Step 1: Get the dictionary to see what's available
$dictionary = $client->puzzle()->dictionary();
print_r($dictionary);

// Step 2: Get all puzzles to see what's in the bundle
$allPuzzles = $client->puzzle()->collect();
echo "Total puzzles: " . count($allPuzzles) . "\n";

// Step 3: Check a specific puzzle's properties
if (!empty($allPuzzles)) {
    print_r($allPuzzles[0]);
}

// Step 4: Try filtering by something you know exists
$firstPuzzle = $allPuzzles[0];
$filtered = $client->puzzle()->collect([
    'puzzleDate' => $firstPuzzle['rdate'],
    'puzzleTypes' => [$firstPuzzle['abbr']]
]);
echo "Filtered results: " . count($filtered) . "\n";
bash
./vendor/bin/phpunit tests/Integration/CredentialsTest.php