brd6 / notion-sdk-php
Notion SDK for PHP
Fund package maintenance!
brd6
Installs: 6 557
Dependents: 0
Suggesters: 0
Security: 0
Stars: 56
Watchers: 5
Forks: 6
Open Issues: 1
Requires
- php: ^7.4 || ^8
- php-http/client-common: ^2.5
- php-http/discovery: ^1.14
- php-http/httplug: ^2.3
- php-http/message-factory: ^1.0
- psr/http-client-implementation: ^1.0
- psr/http-message: ^2.0
Requires (Dev)
- captainhook/captainhook: ^5.10
- dealerdirect/phpcodesniffer-composer-installer: ^1.0.0
- ergebnis/composer-normalize: ^2.25
- hamcrest/hamcrest-php: ^2.0
- mockery/mockery: ^1.5
- nyholm/psr7: ^1.5
- php-http/message: ^1.13
- php-parallel-lint/php-console-highlighter: ^1.0
- php-parallel-lint/php-parallel-lint: ^1.3
- phpstan/phpstan: ^1.5
- phpstan/phpstan-mockery: ^1.0
- phpstan/phpstan-phpunit: ^1.1
- phpunit/phpunit: ^9.5
- psalm/plugin-mockery: ^1.1.0
- psalm/plugin-phpunit: ^0.19.0
- ramsey/coding-standard: ^2.0
- roave/security-advisories: dev-latest
- symfony/http-client: ^5.4
- vimeo/psalm: ^5.6
Suggests
- nyholm/psr7: PSR-7 message implementation
- symfony/http-client: HTTP client
- dev-main
- 1.5.0
- 1.4.0
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.0
- dev-feat/mention-custom-emoji-block
- dev-dependabot/github_actions/codecov/codecov-action-5.0.7
- dev-feat/custom-emoji-block
- dev-fix/table-cell-content
- dev-fix/table-row-empty-cells
- dev-feature/audio-block
- dev-feature/file-caption
- dev-fix/float-number-property-value
- dev-feature/status-property
- dev-fix/allow-empty-content
- dev-database-filters
This package is auto-updated.
Last update: 2024-12-10 07:07:01 UTC
README
Notion SDK for PHP
PHP version of the official NOTION API. It works the same way as the reference JavaScript SDK 🎉
Installation
Install this package as a dependency using Composer.
composer require brd6/notion-sdk-php
This package (brd6/notion-sdk-php)
is not tied to any specific library such as Guzzle or Buzz that sends HTTP messages. Instead, it uses the PSR-18 client abstraction to let users choose whichever PSR-7 implementation
and HTTP client they want to use.
If you just want to get started quickly with symfony http client, run the following command:
composer require brd6/notion-sdk-php symfony/http-client nyholm/psr7
Usage
Use Notion's Getting Started Guide to get set up to use Notion's API.
Import and initialize a client using an integration token or an OAuth access token.
use Brd6\NotionSdkPhp\Client; use Brd6\NotionSdkPhp\ClientOptions; $options = (new ClientOptions()) ->setAuth(getenv('NOTION_TOKEN')); $notion = new Client($options);
Make a request to any Notion API endpoint.
See the complete list of endpoints in the API reference.
$listUsersResponse = $notion->users()->list(); var_dump($listUsersResponse->toArray());
array (size=4) 'has_more' => boolean false 'results' => array (size=2) 0 => array (size=6) 'object' => string 'user' (length=4) 'id' => string '7f03dda0-a132-49d7-b8b2-29c9ed1b1f0e' (length=36) 'type' => string 'person' (length=6) 'name' => string 'John Doe' (length=8) 'avatar_url' => string 'https://s3-us-west-2.amazonaws.com/public.notion-static.com/521dfe9c-f821-4de8-a0bb-e40ff71283e5/39989484_10217003981481443_4621803518267752448_n.jpg' (length=149) 'person' => array (size=1) ... 1 => array (size=5) 'object' => string 'user' (length=4) 'id' => string '8dee9e49-7369-4a6d-a11f-7db625b2448c' (length=36) 'type' => string 'bot' (length=3) 'name' => string 'MyBot' (length=5) 'bot' => array (size=1) ... 'object' => string 'list' (length=4) 'type' => string 'user' (length=4)
Endpoint parameters are grouped into a single object. You don't need to remember which parameters go in the path, query, or body.
$databaseRequest = new DatabaseRequest(); $databaseRequest->setFilter([ 'property' => 'Landmark', 'text' => [ 'contains' => 'Bridge', ], ]); $myPage = $notion->databases()->query('897e5a76-ae52-4b48-9fdf-e71f5945d1af', $databaseRequest)
Handling errors
If the API returns an unsuccessful response, an ApiResponseException
will be thrown.
The error contains properties from the response, and the most helpful is code
. You can compare code
to the values in the NotionErrorCodeConstant
object to avoid misspelling error codes.
Client options
The Client
supports the following options on initialization. These options can be set on the ClientOptions
instance.
Contributing
Contributions are welcome! To contribute, please familiarize yourself with CONTRIBUTING.md.
License
The MIT License (MIT). Please see LICENSE for more information.