97 lines
2.7 KiB
PHP
97 lines
2.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
class NotionClient {
|
||
|
|
private $apiKey;
|
||
|
|
private $version = '2025-09-03';
|
||
|
|
private $baseUrl = 'https://api.notion.com/v1';
|
||
|
|
|
||
|
|
public function __construct($apiKey) {
|
||
|
|
$this->apiKey = trim($apiKey);
|
||
|
|
}
|
||
|
|
|
||
|
|
private function makeRequest($endpoint, $method = 'GET', $data = null) {
|
||
|
|
$ch = curl_init();
|
||
|
|
$url = $this->baseUrl . $endpoint;
|
||
|
|
|
||
|
|
$headers = [
|
||
|
|
'Authorization: Bearer ' . $this->apiKey,
|
||
|
|
'Notion-Version: ' . $this->version,
|
||
|
|
'Content-Type: application/json'
|
||
|
|
];
|
||
|
|
|
||
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||
|
|
|
||
|
|
if ($method === 'POST') {
|
||
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
||
|
|
if ($data) {
|
||
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$response = curl_exec($ch);
|
||
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||
|
|
|
||
|
|
if (curl_errno($ch)) {
|
||
|
|
throw new Exception("Curl Error: " . curl_error($ch));
|
||
|
|
}
|
||
|
|
|
||
|
|
curl_close($ch);
|
||
|
|
|
||
|
|
if ($httpCode >= 400) {
|
||
|
|
// For debugging purposes, you might want to log the response
|
||
|
|
error_log("Notion API Error ($httpCode): $response");
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
return json_decode($response, true);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function search($query) {
|
||
|
|
$data = [
|
||
|
|
'query' => $query,
|
||
|
|
'page_size' => 3, // Limit to top 3 results
|
||
|
|
'filter' => [
|
||
|
|
'value' => 'page',
|
||
|
|
'property' => 'object'
|
||
|
|
],
|
||
|
|
'sort' => [
|
||
|
|
'direction' => 'descending',
|
||
|
|
'timestamp' => 'last_edited_time'
|
||
|
|
]
|
||
|
|
];
|
||
|
|
|
||
|
|
return $this->makeRequest('/search', 'POST', $data);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getPageContent($pageId) {
|
||
|
|
$response = $this->makeRequest("/blocks/$pageId/children", 'GET');
|
||
|
|
|
||
|
|
if (!$response || !isset($response['results'])) {
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
|
||
|
|
$content = "";
|
||
|
|
$maxLength = 1500; // Limit content length to avoid token limits
|
||
|
|
|
||
|
|
foreach ($response['results'] as $block) {
|
||
|
|
if (strlen($content) >= $maxLength) {
|
||
|
|
$content .= "...(내용 생략됨)...";
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
$type = $block['type'];
|
||
|
|
if (isset($block[$type]) && isset($block[$type]['rich_text'])) {
|
||
|
|
foreach ($block[$type]['rich_text'] as $text) {
|
||
|
|
$content .= $text['plain_text'];
|
||
|
|
}
|
||
|
|
$content .= "\n";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return $content;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
?>
|