lapalabs/youtube-helper

A tiny package to convenience work with YouTube media resources. Allow to extract ID from resource URL and build valid resource URLs.

v0.1.0 2015-07-13 09:13 UTC

This package is auto-updated.

Last update: 2024-10-29 04:36:29 UTC


README

A tiny package to convenience work with YouTube media resources. Allow to extract ID from resource URL and build valid resource URLs.

SensioLabsInsight Build Status Scrutinizer Code Quality Code Coverage

Installation

Install package to your project with Composer:

$ composer require lapalabs/youtube-helper dev-master

Usage

Creation

You can easily create valid YouTube resource object:

use LapaLabs\YoutubeExtractor\Resource\YoutubeResource;

// Build resource object from valid YouTube resource ID
$resource = new YoutubeResource('5qanlirrRWs');
// or with static method
$resource = YoutubeResource::create('5qanlirrRWs');

// or create from valid YouTube resource URL
$resource = YoutubeResource::createFromUrl('https://www.youtube.com/watch?v=5qanlirrRWs');

There are a few valid YouTube resource URLs, supported by this library, that should be used in YoutubeResource::createFromUrl() method:

  • https://youtube.com/watch?v=5qanlirrRWs
  • https://m.youtube.com/watch?v=5qanlirrRWs
  • https://www.youtube.com/watch?v=5qanlirrRWs
  • https://www.youtube.com/embed/5qanlirrRWs
  • https://youtu.be/5qanlirrRWs

Advanced usage

After resource was successfully created you get access to a bunch of useful methods:

$resource->getId();           // 5qanlirrRWs
$resource->buildEmbedUrl();   // https://www.youtube.com/embed/5qanlirrRWs

// other useful methods to build various valid URLs
$resource->buildUrl();        // shortcut alias for buildDefaultUrl
$resource->buildDefaultUrl(); // https://www.youtube.com/watch?v=5qanlirrRWs
$resource->buildAliasUrl();   // https://youtube.com/watch?v=5qanlirrRWs
$resource->buildMobileUrl();  // https://m.youtube.com/watch?v=5qanlirrRWs
$resource->buildShortUrl();   // https://youtu.be/5qanlirrRWs

You can get array of valid YouTube resource URLs which could used in createFromUrl method:

YoutubeResource::getValidHosts(); // array of valid YouTube resource URLs

To check whether YouTube resource ID or host is valid use follow methods:

YoutubeResource::isValidId('5qanlirrRWs'); // return true if ID is valid
YoutubeResource::isValidHost('youtu.be');  // return true if host is valid

You can easily get HTML code to embed YouTube resource on your page:

$resource->buildEmbedCode(); // with default attributes returns: <iframe width="560" height="315" src="https://www.youtube.com/embed/5qanlirrRWs" frameborder="0" allowfullscreen></iframe>

// to pass any other parameters or override defaults with your own use:
$resource->buildEmbedCode([
    'width' => 800,     // override default 560
    'height' => 600,    // override default 315
    'class' => 'video', // add new attribute 
]);

The passed attributes to buildEmbedCode() methods applies for current embed HTML code only. To change default attributes globally for specific resource you should pass an array of attributes to setAttributes() method. To get current default HTML attributes of specific resource use getAttributes() method.

There are a few attributes by default:

[
    'width'           => 560,
    'height'          => 315,
    'src'             => '', // hold position for specific order
    'frameborder'     => 0,
    'allowfullscreen' => null,
];

NOTE: The src attribute is only required for building embed HTML code and will be injected automatically by calling buildEmbedUrl() method behind the scenes. So every given src value to buildEmbedCode() or setAttributes() will be ignoring and replacing with correct URL for current resource. You could give it like in example above to point its position in HTML code.

Links

Feel free to create an Issue or Pull Request if you find a bug or just want to propose improvement suggestion.

Move UP