PHP code example of joeyboli / radioapisdk

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

    

joeyboli / radioapisdk example snippets


use RadioAPI\RadioAPI;

// Set up your client
$api = RadioAPI::make('https://api.radioapi.io', 'your-api-key')
    ->service(RadioAPI::SPOTIFY)
    ->withHistory();

// Find out what's playing on a stream
$stream = $api->getStreamTitle('https://stream.example.com/radio');

if ($stream->isSuccess()) {
    $track = $stream->getCurrentTrack();
    echo "Now playing: {$track['artist']} - {$track['song']}\n";
    
    foreach ($stream->getHistory() as $historical) {
        echo "Previously: {$historical['artist']} - {$historical['song']}\n";
    }
}

use RadioAPI\RadioAPI;

// Simple instantiation
$api = new RadioAPI('https://api.radioapi.io', 'your-api-key');

// Or using the fluent factory
$api = RadioAPI::make('https://api.radioapi.io', 'your-api-key')
    ->language('en')           // Response language (ISO 639-1)
    ->service(RadioAPI::AUTO)  // Default service/platform
    ->withHistory()            // Include track history by default
    ->timeout(60);             // Request timeout in seconds

$response = $api->getStreamTitle(
    streamUrl: 'https://stream.example.com/radio',
    service: RadioAPI::SPOTIFY, // Optional override
    withHistory: true           // Optional override
);

if ($response->isSuccess()) {
    echo $response->getArtist() . ' - ' . $response->getTitle();
}

$response = $api->searchMusic(
    query: 'The Beatles - Hey Jude',
    service: RadioAPI::SPOTIFY, // Optional
    language: 'en'              // Optional
);

foreach ($response->getTracks() as $track) {
    echo "{$track['artist']} - {$track['title']}\n";
}

$colors = $api->getImageColors('https://example.com/album-art.jpg');

if ($colors->isSuccess()) {
    echo "Dominant Hex: " . $colors->getDominantColorHex();
    echo "Text Color Hex: " . $colors->getTextColorHex();
}

$endpoint = $api->createMetadataEndpoint(
    kind: 'jcplayer',
    url: 'https://your-stream-url',
    options: [
        'service' => RadioAPI::AUTO,
        'locale'  => 'en',
        'history' => true,
    ]
);

$uuid = $endpoint->getUuid();
echo "Endpoint URL: " . $endpoint->getEndpointUrl();

// Fetch current metadata
$stream = $api->getMetadata($uuid, ['locale' => 'fr']);

// Fetch track history
$history = $api->getMetadataHistory($uuid, ['limit' => 20]);

// Fetch most played tracks
$popular = $api->getMetadataPopular($uuid, ['limit' => 10]);

// General stats for all your endpoints
$globalStats = $api->getMetadataStats(['days' => 30]);

// Detailed stats for a specific endpoint
$stats = $api->getMetadataEndpointStats($uuid, ['days' => 7]);

echo "Total Requests: " . $stats->getTotalRequests();
print_r($stats->getRequestsByDay());

$endpoint = $api->createMetadataEndpoint('jcplayer', '...');

// Update the endpoint
$endpoint->update(['history' => false]);

// Get live metadata
$metadata = $endpoint->getMetadata();

// Get history or popular tracks
$history = $endpoint->getHistory();
$popular = $endpoint->getPopular();

// Get its specific stats
$stats = $endpoint->getStats();

// Or delete it
$endpoint->delete();

use RadioAPI\Exceptions\RadioAPIException;

try {
    $api->getStreamTitle('invalid-url');
} catch (RadioAPIException $e) {
    if ($e->isRateLimited()) {
        // Handle 429
    } elseif ($e->isUnauthorized()) {
        // Handle 401
    }
    echo "Error: " . $e->getMessage();
}