Requirements
Before installing YTMusicAPI PHP, ensure your system meets the following requirements:
Requirement | Version | Notes |
---|---|---|
PHP | 7.4+ | Developed and tested on PHP 8.2 |
Composer | 2.0+ | For package management |
cURL extension | - | Required for HTTP requests |
JSON extension | - | Required for API communication |
Composer Installation
The easiest way to install YTMusicAPI PHP is through Composer:
composer require ytmusicapi/ytmusicapi
This will automatically download the library and its dependencies, including the rmccue/requests package used for HTTP communication.
Verify Installation
Create a simple test script to verify the installation:
<?php
require "vendor/autoload.php";
try {
$yt = new Ytmusicapi\YTMusic();
echo "YTMusicAPI PHP is installed correctly!\n";
// Test a simple search
$results = $yt->search("test", limit: 1);
echo "Search functionality works: " . count($results) . " result(s) found\n";
print_r($results);
} catch (Exception $e) {
echo "Installation error: " . $e->getMessage() . "\n";
}
Save the file as test-installation.php.
Run this script with:
php test-installation.php
Autoloading
YTMusicAPI PHP uses a custom autoloading approach to stay close to the original Python structure. The main class and all mixins are automatically included when you include the Composer autoloader.
<?php
require "vendor/autoload.php";
// Now you can use any part of the library
$yt = new Ytmusicapi\YTMusic();
$results = $yt->search("your query");
Authentication
To access your personal YouTube Music data (playlists, library, history), you need to authenticate your requests. Please see the authentication page for more information.
Troubleshooting
Common Issues
Class 'Ytmusicapi\YTMusic' not found
Solution: Make sure you've run composer install and included the autoloader:
require "vendor/autoload.php";
cURL Error: Could not resolve host
Solution: This is usually a network connectivity issue. Ensure your server can reach YouTube Music servers and that cURL is properly configured.
HTTP 403 Forbidden Errors
Solution: Google has been stricter with bot detection. Try using authentication or consider if you're running on a commonly flagged hosting provider, which is most of them.
SSL Certificate Issues
Solution: Update your CA bundle or configure cURL to handle SSL properly:
// If you encounter SSL issues, you may need to configure the session
$session = new \WpOrg\Requests\Session();
$session->options["verify"] = "/path/to/cacert.pem";
$yt = new Ytmusicapi\YTMusic(null, null, $session);
Getting Help
If you encounter issues not covered here:
- Check the GitHub issues
- Refer to the original Python documentation
- Review the examples for working code patterns