LoginSignup
6
6

More than 3 years have passed since last update.

[PHP] ScrapboxAPIを使って最新の投稿を取得する

Last updated at Posted at 2017-12-11

Scrapboxについて

Scrapboxは情報を記録するためのサービスです。
技術的な知識やデザインの知識をはじめ、近くのカフェの情報などなんでも記録できます。
Evernoteとは違い、テキスト中にソースコードを綺麗に表示できる機能などもあったりします。
自分は今後、Scrapboxを技術メモとして使う(使っていく予定)です。

APIを簡単に試す

まずはcurlコマンドを使って試してみます。
ページリストの取得には、以下にリクエストを送ればOKです。
https://scrapbox.io/api/pages/:projectname
認証などは必要なくとても簡単です。
他のAPIなどはこちらをチェック!
実際に実行してみた結果を以下にようになります。1

$ curl https://scrapbox.io/api/pages/yoneda
{
    "count": 4,
    "limit": 100,
    "pages": [
        {
            "accessed": 1512993738,
            "commitId": "5a2e6b50369e800014366dcd",
            "created": 1512985147,
            "descriptions": [],
            "id": "5a2e522e369e8000143662f0",
            "image": null,
            "linked": 0,
            "pin": 0,
            "point": 19,
            "title": "AWSEC2\u3067\u30c7\u30a3\u30b9\u30af\u5bb9\u91cf\u306e\u4f7f\u7528\u7387\u3092\u898b\u308b",
            "updated": 1512991568,
            "user": "5a0b16bc5fccc70014d542be",
            "views": 18
        },...

APIで最新の投稿を取得

phpで最近投稿されたノートのタイトルを表示します。
cURLモジュールを利用しています。

scrapbox.php
<?php
// cURLモジュールを使ってAPIにリクエストを送る
$url = "https://scrapbox.io/api/pages/yoneda/?limit=3";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url); // urlを設定
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET"); // GETリクエストにする
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  // 変数に情報を返す(これがfalseだとコンソールに値が出力されてしまう)
$results = curl_exec($curl);
curl_close($curl);

// タイトルを表示
$decodedResults = json_decode($results);
$pages = $decodedResults->pages;
for($i=0; $i<count($pages); $i++){
  $title = $pages[$i]->title;
  print($title."\n");
}
?>
$ php scrapbox.php
AWSEC2でディスク容量の使用率を見る
コマンドラインでjsonを整形する方法
powerpointのスライドのサイズ

  1. 2017年12月時点での取得結果です。 

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6