search.php

View the content of the Karaoke-Version sample "search.php", download the file and view its result after execution.

Download file
FileResult
<?php

require_once(__DIR__ . '/KVService.php');

header("Content-Type: text/html; charset=UTF-8");

// Display the form
echo '<h1>Search</h1>';
echo '<form method="POST">';
echo '<label>Query: <input type="text" name="query" value="' . (isset($_POST['query']) ? trim($_POST['query']) : '') . '" /></label>';
echo '<input type="submit" value="Search">';
echo '</form>';

if (isset($_POST['query']) && strlen(trim($_POST['query'])) > 0) {
    $userQuery = trim($_POST['query']);
    // Query to get the Top 10 Songs
    $querySearch = array(
        "function" => "song",
        "parameters" => array("query" => $userQuery)
    );
    // Get the songs back form the service
    $searchResult = KVService::querySearch($querySearch);
    if ($searchResult->length == 0) {
        echo '<p>No result found for query "' . $userQuery . '".</p>';
    } else {
        echo "<ol>";
        foreach ($searchResult->songs as $song) {
            // Get the artist of the song
            $queryArtist = array(
                "function" => "get",
                "parameters" => array("id" => $song->artistId)
            );
            $artist = KVService::queryArtist($queryArtist)->artist;
            echo '<li>
                    <a href="' . $song->url . '">' . $artist->name . ' - ' . $song->name . '</a> | <a href="' . $song->previewUrl . '">Preview</a>
                  </li>';
        }
        echo "</ol>";
    }
}