keinos/mastodon-streaming-api-listener

Listens to the server-sent messages of Mastodon Streaming API and returns the events in JSON.

1.0.1 2020-12-17 02:50 UTC

This package is auto-updated.

Last update: 2024-10-23 13:32:33 UTC


README

Simple Mastodon Streaming API Listener

This is a PHP class that listens to the Mastodon Streaming API's server-sent messages.

Install

composer require keinos/mastodon-streaming-api-listener

Usage

<?php

require_once __DIR__ . '/../vendor/autoload.php';

$conf = [
    // Your Mastodon server URL
    'url_host' => 'https://qiitadon.com/',
];

$listener = new \KEINOS\MSTDN_TOOLS\Listener\Listener($conf);

/**
 * $listener ............ The iterator.
 *   $event_name ........ Event name. ("update" or "delete")
 *   $data_payload ...... Data of the event in JSON string.
 */
foreach($listener as $event_name => $data_payload) {
    echo 'Event name: ' . $event_name . PHP_EOL;
    echo 'Data: '. PHP_EOL;
    print_r(json_decode($data_payload));
}
<?php

require_once __DIR__ . '/../vendor/autoload.php';

// Alias \KEINOS\MSTDN_TOOLS\Listener\Listener as Listener
use KEINOS\MSTDN_TOOLS\Listener\Listener;

$conf = [
    'url_host' => 'https://qiitadon.com/',
    // If the server is in "whitelist-mode" then you'll need an access token.
    'access_token' => 'YOUR_ACCESS_TOKEN',
];

$listener = new Listener($conf);

foreach($listener as $event_name => $data_payload) {
    echo 'Event name: ' . $event_name . PHP_EOL;
    echo 'Data: '. PHP_EOL;
    print_r(json_decode($data_payload));
}
<?php

require_once __DIR__ . '/../vendor/autoload.php';

use KEINOS\MSTDN_TOOLS\Listener\Listener;

$conf = [
    'url_host' => 'https://qiitadon.com/',
    // To listen the local time line stream set 'local'. 'public' is the default.
    'type_stream' => 'local',
];

$listener = new Listener($conf);

foreach($listener as $event_name => $data_payload) {
    echo 'Event name: ' . $event_name . PHP_EOL;
    echo 'Data: '. PHP_EOL;
    print_r(json_decode($data_payload));
}

Package Information