Skip to content

Instantly share code, notes, and snippets.

@brandonkramer
Last active October 17, 2023 21:18
Show Gist options
  • Save brandonkramer/c5f872bfb6dedbacfeaab3f1bd2699b4 to your computer and use it in GitHub Desktop.
Save brandonkramer/c5f872bfb6dedbacfeaab3f1bd2699b4 to your computer and use it in GitHub Desktop.
(Notion.so) Notion API Integration (PHP) - A simple snippet which tells you, how to add a new page to a Notion database with PHP using Curl and their public API. You need a secret token and the ID of a "Shared" database. In the example below the database needs to have a text property called "Description" and the default "Name" property.
<?php
// Options
$secret_token = 'YOUR_SECRET_TOKEN';
$database_id = 'YOUR_DATABASE_ID';
$notion_version = '2021-08-16';
// Post url
$post_url = 'https://api.notion.com/v1/pages';
// Post fields
$post_fields = [
'parent' => [ 'database_id' => $database_id, ],
'properties' => [
'Name' => [
'title' => [
[
'text' => [
'content' => 'The page name',
],
],
],
],
'Description' => [
'rich_text' => [
[
'text' => [
'content' => 'A dark green leafy vegetable',
],
],
],
],
],
];
// Init curl
$ch = curl_init();
// Set curl options
curl_setopt_array( $ch, [
CURLOPT_URL => $post_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode($post_fields),
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
"Authorization: Bearer {$secret_token}",
"Notion-Version: {$notion_version}",
],
] );
// Get response
$response = curl_exec( $ch );
// Close curl
curl_close( $ch );
echo $response;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment