LoginSignup
5
2

More than 3 years have passed since last update.

Mastodon API からローカルタイムラインを取得する(「LTL」取得の 1 行サンプル)

Last updated at Posted at 2018-02-09

マストドンのローカルタイムラインを curl で取得する

TL;DR

タイムラインのエンドポイントのクエリ引数にlocal=hogeを加える。(hogeは何でもよい)

LTLのcURL構文
$ curl -X $method -sS $schema://$host$endpoint?local=true
Qiitadonの例
$ curl -X GET -sS https://qiitadon.com/api/v1/timelines/public?local=something

なお、ローカルタイムライン(LTL)と連合タイムライン(FTL)はアクセストークンを必要としません。

cURL を叩きすぎると一時的にブロックされるので、毎回断続的に cURL を叩かずにストリーミングとして取得したい(つなげっぱでリアルタイムに取得したい)場合は Streaming API を利用します。

TS;DR

連合タイムラインと同じエンドポイントですが、違うのはエンドポイントのクエリの引数に local キーと空白以外の値を入れてリクエストすることです。(値は空でない文字列であれば何でもよい)

つまり Qiitadon の場合 https://qiitadon.com/api/v1/timelines/public?local=hoge にリクエストします。

上記 cURL 文を PHP から外部コマンドとして実行する場合のサンプルです。

sample.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
  • HighSierra (OSX 10.13.5)
  • PHP 7.2.6 (cli)
  • curl 7.54.0 (x86_64-apple-darwin17.0)
    • libcurl/7.54.0
    • LibreSSL/2.0.20
    • zlib/1.2.11
    • nghttp2/1.24.0

Raspbian Jessie
  • Raspbian Jessie(Debian V8)
  • PHP 7.0.27-0+deb9u1 (cli)
  • curl 7.38.0 (arm-unknown-linux-gnueabihf)
    • libcurl/7.38.0
    • OpenSSL/1.0.1t
    • zlib/1.2.8
    • libidn/1.29
    • libssh2/1.4.3
    • librtmp/2.3

CentOS 7
  • CentOS Linux release 7.5.1804 (Core)
  • PHP 5.6.36 (cli)
  • curl 7.29.0 (x86_64-redhat-linux-gnu)
    • libcurl/7.29.0
    • NSS/3.34 zlib/1.2.7
    • libidn/1.28
    • libssh2/1.4.3

関連記事

参考記事

5
2
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
5
2