PHP code example of keinos / mastodon-streaming-api-parser

1. Go to this page and download the library: Download keinos/mastodon-streaming-api-parser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

keinos / mastodon-streaming-api-parser example snippets


use \KEINOS\MSTDN_TOOLS\Parser\Parser;

$parser = new Parser();

while (! feof($stream)) {
    $line = fgets($stream);
    $json = $parser->parse($line);
    if (false === $json) {
        continue;
    }
    echo $json . PHP_EOL;
}

    $parser = new \KEINOS\MSTDN_TOOLS\Parser\Parser();
    

    /**
     * @param  string $line   Received streaming line.
     * @return bool|string    Returns the data unit in JSON string. Or false if the
     *                        status is "buffering in progress".
     */
    $json = $parser->parse($line);
    

// Instantiate the parser
$parser = new \KEINOS\MSTDN_TOOLS\Parser\Parser();
// Open socket
$fp = fsockopen($hostname, $port, $errno, $errstr, $timeout);
// Send GET request
fwrite($fp, $req);
// Looper
while (! feof($fp)) {
    // Read the stream
    $read = fgets($fp);
    // Buffer each line until it returns the data
    $json = $parser->parse($read);
    if (false === $json) {
        continue;
    }
    // Do something with the data
    echo $json . PHP_EOL;
}
// Close connection
fclose($fp);