マストドンのローカルタイムラインを curl
で取得する
TL;DR
タイムラインのエンドポイントのクエリ引数に
local=hoge
を加える。(hoge
は何でもよい)
$ curl -X $method -sS $schema://$host$endpoint?local=true
$ curl -X GET -sS https://qiitadon.com/api/v1/timelines/public?local=something
- オンラインで動作を見る @ paiza.IO
なお、ローカルタイムライン(LTL)と連合タイムライン(FTL)はアクセストークンを必要としません。
cURL を叩きすぎると一時的にブロックされるので、毎回断続的に cURL を叩かずにストリーミングとして取得したい(つなげっぱでリアルタイムに取得したい)場合は Streaming API を利用します。
TS;DR
連合タイムラインと同じエンドポイントですが、違うのはエンドポイントのクエリの引数に local
キーと空白以外の値を入れてリクエストすることです。(値は空でない文字列であれば何でもよい)
つまり Qiitadon の場合 https://qiitadon.com/api/v1/timelines/public?local=hoge
にリクエストします。
上記 cURL 文を PHP から外部コマンドとして実行する場合のサンプルです。
<?php
$schema = 'https';
$host = 'qiitadon.com';
$endpoint = '/api/v1/timelines/public';
$method = 'GET';
$args = 'local=true'; //LocalTimeLine用引数
/* cURL リクエストの作成 */
$query = "curl -X ${method}";
$query .= " -sS ${schema}://${host}${endpoint}?${args};";
/* cURL リクエストの送信 */
$result_json = `$query`; //バッククォートで $query の cURL 実行
$result_array = json_decode(trim($result), JSON_OBJECT_AS_ARRAY);
print_r($result_array);
無理して外部コマンドの cURL
を使わずに file_get_contents()
関数を使う場合。
<?php
$url_request = 'https://qiitadon.com/api/v1/timelines/public?local=something';
$result_raw = file_get_contents($url_request);
$result_array = json_decode(trim($result_raw), JSON_OBJECT_AS_ARRAY);
print_r($result_array);
検証環境(動作確認済み環境)
macOS HighSierra
Raspbian Jessie
CentOS 7
関連記事
- Mastodon API から「返信」をトゥートする 1 行サンプル @ Qiita
- Mastodon API から「未収載」でトゥートする PHP サンプル @ Qiita
- Mastodon API で「You are being redirected」とアクセストークンの取得 @ Qiita
参考記事
- マストドンAPIの解説 @ ヤマサキマサキ.com
- 公式 Qiitadon API | Timelines @ GitHub