출처: https://copyprogramming.com/howto/trying-to-send-query-to-notion-api-using-php
공식 Notion Public API 문서는 “object”, “id”, “created_time”, “last_edited_time”와 같은 속성과 제한된 메타데이터로 페이지를 검색하는 방법의 지침을 제공합니다.
PUT 요청을 수행하려면 payload는 POST 데이터베이스 쿼리 필터를 Notion의 API 문서 페이지의 설명대로 구성되어야 합니다.
보안을 위해 연결 정보의 코드는 생략되었음을 반드시 참고(인지)해주시길 바랍니다.
그러나 생략된 것은 당면한 문제와는 관련이 없습니다.
(but it is not relevant to the issue at hand.)
아래 링크를 확인해 주세요.
https://developers.notion.com/reference/post-database-query for more information.
나는 Notion API와 PHP를 사용하여 Notion 데이터베이스를 쿼리하는 것을 시도한다.
나는 나의 간과된 POST 요청의 문제를 직면한다.
나는 API에 성공적으로 연결되었음에도 불구하고 어떤 방식으로든 나에게 도움 되지 않는 엄청난 양의 데이터를 받습니다.
이 특정 POST 요청에 대한 Notion의 API 문서 페이지에 따르면, https://developers.notion.com/reference/ post-database -query#post- database-query -filter에 찾을 수 있고 payload 포맷은 반드시 아래처럼 작성해야 한다.
(According to the API documentation page of Notion for this particular POST request, which can be found at https://developers.notion.com/reference/ post-database -query#post- database-query -filter, the payload format should be as described below.)
{
"property": "Landmark",
"text": {
"contains": "Bridge"
}
}
현재 PUT 작업을 실행하기위한 나의 코드는 아래와 같이 쓰여 졌습니다.
//connection information
$databaseId = "zzz";
$url = "https://api.notion.com/v1/databases/$databaseId/query"; //api endpoint
$token = 'zzx';
$version = '2021-08-16';
//post data
$data_array =
[
"property"=>"DataElement",
"title"=>["equals"=>"bigHouse"]
];
$data = json_encode($data_array);
//intialize cURL
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//below is for posting
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $token,
'Notion-Version: '.$version));
$resp = curl_exec($ch);
if($e = curl_error($ch))
{
echo $e;
} else
{
$decoded = json_decode($resp, true);
var_dump($decoded);
}
//file_put_contents('test.json',json_encode($decoded, JSON_PRETTY_PRINT));
echo "
".$data;
curl_close($ch);
?>
보안을 위해 나는 연경 정보를 제외했다. 문제는 그것과 관련이 없습니다. 나는 POST 요청으로부터 모든 json 데이터를 받지만 나의 목표는 내 payload에 해당하는 데이터만 얻는 것입니다. echo와 var_dump와 같은 코드의 마지막줄은 테스트 목적으로 활용됩니다. echo $data를 실행하면 아래와 같이 얻습니다.
{"property":"DataElement","title":{"equals":"bigHouse"}}
Notion API의 형식 요구 사항에 따라 “Title” type으로 분류되는 " datael ement”란 이름의 Notion Database의 초기의 column 쿼리하는 것을 시도하고 있다.
(I am attempting to query the initial column of a Notion Database named " datael ement" that is categorized as a "Title" type, as per the formatting requirements for Notion's API.)
해결 방법:
파라미터인 filter는 반드시 필터를 포함해야한다.
(The parameter filter must contain the filter.)
$data_array =
['filter' =>
[
"property"=>"DataElement",
"title"=>["equals"=>"bigHouse"]
]
];
post-database-query 문서를 언급하는 https://developers.notion.com/reference/post-database-query 에서 이용할 수 있습니다.
(Refer to the post-database-query documentation available at https://developers.notion.com/reference/post-database-query.)
HTTP를 통해 PHP에 데이터를 post하고 실행하는 C# 방법,
(C# how to post data through HTTP to PHP and run it,)
나는 코드를 서버에 ftp로 이용했지만 사용하는 것과 기타 등등을 직접 해야 한다는 점을 유념하세요.
(Note you'll have to do the using and whatnot like you are but I have used that code to ftp to a server.)
나는 너의 변수 이름들을 사용하려고 약간 수정했습니다.(I modified it slightly to try and use your variable names though.) 요청은 HTTP를 사용하여 파일 이름만 보내는 것이었으므로 이를 내 답변에 추가했습니다.
(Since the request was to use HTTP to send just a filename I've added that to my answer.)
아래 코드에서 큰 부분은 url이라고 저는 추정합니다…. (원문에서는 uri라고 했으나 오타인듯하여 url로 수정하였습니다.)
(In the code below the big part is the uri. I am assuming …)
페이지 검색에 대한 Notion Public API의 공식 문서에 따르면, 페이지 속성과 "object", "id", "created_time" 및 "last_edited_time"과 같은 일부 메타데이터에 접근할 수 있습니다. 사용 가능한 메타데이터에서 페이지 슬러그 또는 URL을 검색하는 방법을 잘 모르겠습니다.(However, I am unsure how to retrieve the page slug or URL from the available metadata.) 내 목표는 특정 데이터베이스 항목에 대한 페이지 URL을 얻는 것입니다.
해결 방법:
https://api.notion.com/v1/databases/database_id/query 를 활용하여 데이터베이스를 검색하고 result[].id로 식별된 응답 목록 내에서 페이지 id를 찾으세요.
페이지 URL을 구성할때, results[].id 에서 dash(-)들을 제외하세요. 너의 페이지 URL은 https://notion.so/page_id 에서 이용할 수 있다.
Json - PHP를 사용하여 api로부터 데이터를 가져오기, PHP를 사용하여 api에서 데이터를 가져오는 작업이 있습니다.
POST 요청을 사용하고 json body 전송도 합니다. 나를 혼란스럽게하는 어떠한 header들을 설정하는 것의 언급도 없습니다. 나는 ajax를 사용하여 api를 처리했습니다.
나는 특정 작접을 수행하기 위해 새로운 서비스를 경험하고 있습니다. 그 서비스는 복잡하지 않은 애플리케이션을 사용자가 구성할 수 있는 기본적인 API를 제공합니다. 아래는 HTTP 요청인 하나의 인스턴스입니다.
https://domainname.com/dashboard/api
?to={PHONE NUMBER}&from={SENDER ID}&message={TEXT}
&email={YOUR EMAIL}&api_secret={API SECRET}
&unicode={TRUE/FALSE}&id={IDENTIFIER}
3시간 동안 저는 다양한 메소드와 Postman과 PHP를 활용하고 구글링을 하는 등 다양한 방법을 시도했지만 이 것을 작동할 수 없었고 브라우저를 통해 이 것을 전송하려고 시도했지만 실패했습니다.
http 요청을 보내는 적절한 방법이 무엇일까요?
php 5버전에서 HTTP 요청을 하는 것은 CURL을 사용하지 않아도 됩니다. 이 팁(tip)은 이 팁은 PHP로 게시물 요청을 어떻게 보내나요?에 제공된 솔루션에서 파생되었습니다.
(This tip is derived from the solution provided in How do I send a post request with php ?. )
간단하게 너의 변수들을 $data 배열에 입력하세요.
$url = 'https://domainname.com/dashboard/api';
$data = array('to' => PHONE_NUMBER, 'from' => SENDER_ID, 'message' => TEXT, 'email' => EMAIL, 'api_secret' => SECRET, 'unicode' => BOOLEAN, 'id' => IDENTIFIER);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }
var_dump($result);
너의 스크립트 요청이 제대로 작동 하는지 확인하려면, 너는 http://www.postb.in 와 같은 서비스를 활용할 수 있습니다.
이 것(http://www.postb.in)은 오로지 피드백이나 에러에만 의존하는 대신 요청을 검사하여 디버깅할 수 있습니다. 이렇게 하면 요청의 형식이 적절하고 작동하는지 확인할 수 있습니다.
이는 서비스가 제대로 작동하지 않는 경우 문제 해결에 소요되는 시간과 노력을 크게 절약할 수 있으므로 특히 중요합니다.