PHP code example of ampache / ampacheapi-php

1. Go to this page and download the library: Download ampache/ampacheapi-php 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/ */

    

ampache / ampacheapi-php example snippets


$ampache = new AmpacheApi(array(
   'username' => 'user1', // Username
   'password' => 'test', // Password
   'server' => 'localhost', // Server address, without http/https prefix
   'debug_callback' => 'debug_event', // server callback function
   'api_secure' => 'false' // Set to true to use https
   'api_version' => 6 // Set API response version. 3, 4, 5, 6 (default: 6)
   'api_format' => 'xml' // Set API response format. xml, json (default: json)
));

if ($ampache->state() != 'CONNECTED') {
  echo "Ampache API client failed to connected.\n";
  exit;
}

$stats = $ampache->info();
echo "Songs: " . $stats->songs . "<br />\n";
echo "Albums: " . $stats->albums . "<br />\n";
echo "Artists: " . $stats->artists . "<br />\n";
echo "Playlists: " . $stats->playlists . "<br />\n";
echo "Videos: " . $stats->videos . "<br />\n";

$total = $stats->artists;
$step = 500; // Request per 500
$start = 0;

echo "Artists: <br />\n";
while ($total > $start) {
  $artists = $ampache->send_command('artists', array('offset' => $start, 'limit' => $step));
  foreach ($artists as $artist) {
    echo "\t" . $artist->name . "\n"
  }
}