PHP code example of sharkydog / snapcast-client

1. Go to this page and download the library: Download sharkydog/snapcast-client 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/ */

    

sharkydog / snapcast-client example snippets


use SharkyDog\Snapcast;
$snapc = new Snapcast\Client('192.168.0.123', 1705);

$snapc->on('open', function() use($snapc) {
  // second parameter is a \stdClass with request params
  $snapc->call('Client.SetVolume', (object)[
    'id' => '00:21:6a:7d:74:fc',
    'volume' => (object)[
      'muted' => false,
      'percent' => 74
    ]
  ])->then(function(\stdClass $result) {
    // $result is everything in the result object of the json-rpc response message
  })->catch(function(\Exception $e) {
    // error
    // $e->getCode(), $e->getMessage()
  });
});

// listen for Client.OnVolumeChanged notification
$snapc->on('Client.OnVolumeChanged', function(\stdClass $params) {
  // as with the command above,
  // $params holds everything in the params object of the json-rpc notification message
});

$snapc->connect();

// wait to reconnect in seconds, 0 to disable reconnects
public function reconnectInterval(?int $reconn=null): int;
// connect, last non-null timeout is remembered, seconds
public function connect(?int $timeout=null);
// is client currently connecting
public function connecting(): bool;
// is client currently waiting to reconnect
public function reconnecting(): bool;
// is client connected
public function connected(): bool;
// close connection, force means drop connection without waiting for sent data to be flushed
public function disconnect(bool $force=false);
// send command
public function call(string $method, ?\stdClass $params=null, string $id='', int $timeout=10): Promise\PromiseInterface;

use SharkyDog\Snapcast;
$snapc = new Snapcast\Client('192.168.0.123', 1705);
$plapp = new Snapcast\App\RestorePlayerStream(__DIR__.'/data');
$plapp->client($snapc);
$snapc->connect();