This example demonstrates how to use the notion-sdk-php to recursively fetch all blocks on a page and extract their complete text content. This is useful for tasks like indexing page content for search.
The script handles pagination and nested blocks (like toggles or columns) to ensure all text is extracted.
- PHP 7.4 or higher
- Composer
- Notion Integration Token
- Notion Page shared with your integration
Navigate to this directory and install the required packages using Composer:
cd examples/02-parse-text-from-any-block
composer installCreate a .env file by copying the example file:
cp .env.example .envOpen the .env file and add your Notion integration token and page ID:
NOTION_TOKEN="secret_..."
NOTION_PAGE_ID="..."Execute the script from your terminal:
php index.phpThe script will:
- Connect to your Notion workspace
- Recursively fetch all blocks from the specified page
- Extract text content from each block type
- Handle nested blocks (toggles, columns, etc.)
- Output the complete concatenated text content