A basic PHP wrapper for the Youtube Data API v3 (non-OAuth). Designed to let devs easily fetch public data (video, channel and playlist info) from Youtube. No third-party runtime dependencies.
The reason for returning the decoded JSON response directly is that you only need to read the Google API doc to use this library, instead of learning another set of APIs on top of it. Keep it simple.
Some parameters are missing from this library simply because they were not needed at the time. If you want a particular feature, please file an issue.
OAuth endpoints are out of scope: anything requiring an "authorized request" is not supported.
- PHP 7.0 or newer, including PHP 8.5
- the
curlextension - the
jsonextension
Every release is tested on PHP 7.0, 7.1, 7.2, 7.3, 7.4, 8.0, 8.1, 8.2, 8.3, 8.4 and 8.5.
| PHP | Status |
|---|---|
| 7.0 – 7.4 | Supported. All are past end of life; upgrading is strongly recommended. |
| 8.0 – 8.1 | Supported. Both are past end of life. |
| 8.2 – 8.5 | Supported and actively maintained. |
The 1.x line keeps the PHP 7.0 floor so existing installs are not broken. It will be raised in 2.0.
composer require madcoda/php-youtube-apiPlease read the wiki on how to use this library with PHP with composer, Laravel 4 and Laravel 5.
For the functions implemented in this library, see the API reference.
require 'vendor/autoload.php';
$youtube = new Madcoda\Youtube\Youtube(array('key' => 'YOUR API KEY'));
$video = $youtube->getVideoInfo('rie-hPVJ7Sw');The package is auto-discovered on Laravel 5.5 and newer, so there is nothing to register. Publish the config and set your key:
php artisan vendor:publish --provider="Madcoda\Youtube\YoutubeServiceProviderLaravel5"$video = Youtube::getVideoInfo(request('vid', 'dQw4w9WgXcQ'));On Laravel 5.0 to 5.4, register the provider and alias manually in config/app.php:
'providers' => [
Madcoda\Youtube\YoutubeServiceProviderLaravel5::class,
],
'aliases' => [
'Youtube' => Madcoda\Youtube\Facades\Youtube::class,
],The Laravel 4 provider is deprecated and will be removed in 2.0. Laravel 4 has been end of life since 2015, and the provider relies on
package()andbindShared(), both removed in Laravel 5.
The returned JSON is decoded into PHP objects, not arrays. See the "Reference" section of the official API doc for the shape of each resource.
Methods that fetch a single resource return false when nothing matches; methods that
fetch a list return false when the list is empty. API errors are thrown as \Exception
with the Google error code as the exception code.
Requests go through a small transport interface, so you can route them through your own HTTP stack, add retries or logging, or return canned responses in your own tests.
use Madcoda\Youtube\Http\HttpClientInterface;
class MyClient implements HttpClientInterface
{
public function get($url, array $params)
{
// return the raw, undecoded response body
}
}
$youtube = new Madcoda\Youtube\Youtube(array('key' => 'YOUR API KEY'));
$youtube->setHttpClient(new MyClient());Without one, the library uses Madcoda\Youtube\Http\CurlHttpClient, which behaves exactly
as before.
The test suite runs offline against recorded fixtures, so it needs no API key:
composer install
vendor/bin/phpunitA second suite exercises the real API to catch changes in Google's response shapes. It is excluded by default and needs a key:
YOUTUBE_API_KEY=your-key vendor/bin/phpunit --group liveNo composer.lock is committed, which is deliberate: each supported PHP version has to be
free to resolve the PHPUnit major that supports it, from 5.7 on PHP 7.0 up to 11.5 on
PHP 8.5.
For bugs, complaints or suggestions, please file an issue or email jason@madcoda.com.
madcoda/php-youtube-api is licensed under the MIT License.