brd6/notion-sdk-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

brd6/notion-sdk-php

Composer 安装命令:

composer require brd6/notion-sdk-php

包简介

Notion SDK for PHP

README 文档

README

brd6/notion-sdk-php

Notion SDK for PHP

Source Code Download Package PHP Programming Language Read License Build Status

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)

Data sources (Notion API 2025-09-03)

If you use Notion API version 2025-09-03 or newer, the SDK supports data source endpoints via $notion->dataSources(), and page creation with data_source_id parents. When creating a database with this API version, databases()->create() automatically maps properties to the required initial_data_source payload.

File uploads

The SDK supports the Notion File Upload API via $notion->fileUploads(). Uploading a file takes one call — Notion derives the content type from the filename:

$fileUpload = $notion->fileUploads()->upload($contents, 'image.png');

The uploaded file can then be referenced from a block, page icon, or cover. For larger files and imports, the underlying operations are exposed directly:

use Brd6\NotionSdkPhp\Resource\FileUpload\FileUploadRequest;

// import from a public HTTPS URL
$fileUpload = $notion->fileUploads()->create(FileUploadRequest::externalUrl('https://example.com/image.png'));

// multi-part upload (paid workspaces): create, send each part, then complete
$fileUpload = $notion->fileUploads()->create(FileUploadRequest::multiPart(2, 'large.txt'));
$notion->fileUploads()->sendPart($fileUpload->getId(), $firstChunk, 1, 'large.txt');
$notion->fileUploads()->sendPart($fileUpload->getId(), $secondChunk, 2, 'large.txt');
$fileUpload = $notion->fileUploads()->complete($fileUpload->getId());

retrieve() and list() expose upload status. See examples/09-file-uploads-api-smoke for the full flow, including attaching the upload to a page as an image block.

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.

Option Default value Type Description
auth '' string Bearer token for authentication. If left undefined, the auth parameter should be set on each request.
timeout 60 number Number of seconds to wait before throw a RequestTimeoutException
baseUrl "https://api.notion.com" string The root URL for sending API requests. This can be changed to test with a mock server.
httpClient Default Http Client Psr\Http\Client\ClientInterface The Http Client used to make request on the Notion API. This can be change to customize the base Http Client or replace with a mocked Http Client.

Examples

The examples/ directory contains a comprehensive set of usage examples that demonstrate how to use the SDK for various tasks, from basic API calls to complex integrations:

Each example is self-contained with its own dependencies, documentation, and setup instructions.

Contributing

Contributions are welcome! To contribute, please familiarize yourself with CONTRIBUTING.md.

License

The MIT License (MIT). Please see LICENSE for more information.

brd6/notion-sdk-php 适用场景与选型建议

brd6/notion-sdk-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.5k 次下载、GitHub Stars 达 60, 最近一次更新时间为 2022 年 04 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「notion」 「notion-api」 「notion-sdk」 「notion-client」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 brd6/notion-sdk-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 brd6/notion-sdk-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 brd6/notion-sdk-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 23.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 61
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 60
  • Watchers: 4
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-15