Files
fil/docs/snippets/php/api/client_chunk_text.md
Henrik Jess Nielsen b4c07d3693
All checks were successful
Deploy fil (kreuzberg) / deploy (push) Successful in 49s
Nomad changes
2026-06-01 23:40:55 +02:00

1.0 KiB

<?php
declare(strict_types=1);

use GuzzleHttp\Client;

$client = new Client();
$filePath = 'document.pdf';
$fileContent = file_get_contents($filePath);

try {
    $response = $client->post('http://localhost:8000/extract', [
        'multipart' => [
            [
                'name' => 'file',
                'contents' => $fileContent,
                'filename' => basename($filePath),
                'headers' => ['Content-Type' => 'application/pdf'],
            ],
            [
                'name' => 'chunking',
                'contents' => json_encode(['max_characters' => 800, 'overlap' => 100]),
            ],
        ],
    ]);

    $result = json_decode((string)$response->getBody(), true);
    if (isset($result['chunks']) && is_array($result['chunks'])) {
        echo count($result['chunks']) . " chunks\n";
        foreach ($result['chunks'] as $chunk) {
            echo "  " . strlen($chunk['content'] ?? '') . " chars\n";
        }
    }
} catch (Exception $e) {
    echo "Request failed: " . $e->getMessage() . "\n";
}