Menu

Lab Entry

SMVPE – PHP Class to Parse and Embed Social Media Videos

SMVPE (Social Media Video Parse & Embed) is a PHP class that allows you to easily parse and embed a video URL or ID from some of the most popular social media video sites. The basic premise is simple… you input a URL and the script outputs the embed video.

SMVPE currently supports: Break, Daily Motion, Metacafe, Vimeo, and Youtube

View & Download on Github


How To Use

First, you must include or require the SMVPE class file, like so:

<?php include "smvpe.class.php"; ?>

Once SMVPE class is included you’ll have access to all the public methods. Here’s a quick example of how to embed a video using a URL:

<?php
$smvpe = new SMVPE();
$smvpe->embed( 'https://www.youtube.com/watch?v=zlfKdbWwruY' );
?>

You could also use SMVPE without first initializing it by utilizing the init method and chaining methods. For example:

<?php SMVPE::init()->embed( 'https://www.youtube.com/watch?v=zlfKdbWwruY' ); ?>

Setting Options

Options must be an array of key => value sets. Options can be set several different ways, but the easiest would be to just set them when initiating the SMVPE object, like so:

<?php $smvpe = new SMVPE( array( 'height' => '320', 'width' => '480' ) ); ?>

When initiating a new SMVPE object, if the only argument passed is an array, SMVPE will set this as the options and set the source to empty. You could also set a source as well as options when initiating the SMVPE object, like so:

<?php $smvpe = new SMVPE( 'https://www.youtube.com/watch?v=zlfKdbWwruY', array( 'height' => '320', 'width' => '480' ) ); ?>

Another method you could utilize is setOptions. This could come in handy if you are iterating through multiple videos and want to dynamically change various options. It would work like this:

<?php $smvpe->setOptions( array( 'height' => '320', 'width' => '480' ) ); ?>

Available options include “height”, “width”, “container”, and “params”.

Setting Video Parameters

The video parameters are what will be used to set options for the video being requested. This can either be defined as an array or a query string. To set the video parameters, you could either set it as an option, like so:

<?php $smvpe = new SMVPE( 'https://www.youtube.com/watch?v=zlfKdbWwruY', array( 'params' => '?item1=value&item2=value' ) ); ?>

or you can use the “setParameters” method once the SMVPE object has been initiated, like so:

<?php $smvpe->setParameters( '?item1=value&item2=value' ); ?>

Public Methods

init ( mixed $source, array $options ) Allows you to initialize SMVPE without creating a new instance.
setOptions ( array $options ) Takes an array of name => value groups and merges with default or preset values.
setOption ( string $name, mixed $value ) Adds or modifies a single option. Both fields are required.
setSource ( mixed $source, array $options ) Sets the source URL. If $source is an array and $options is not set, $source will be used as $options.
setSourceByID ( string $id, string $site ) Sets the source by video ID rather than URL. $site must be a valid slug specified within getSites()
setParameters ( mixed $params ) Sets the parameters to pass through the embed URL. Can be an array or a query string.
validateURL ( string $url ) Validates URL. Internal use only.
isValid ( string $url ) Checks if URL string contains valid data and return boolean true/false
extractID ( string $source, string $site )
Extracts the video ID from source URL. $site is optional and if not supplied will automatically be found.
getSourceProvider ( string $source )
Evaluates string and gets source provider as a slug.
getEmbedCode ( string $source, string $site ) Returns the generated embed code in HTML form. $site is optional and if not supplied will automatically be found.
getEmbedURL ( string $source, string $site ) Returns the generated URL used to embed video in an iframe.
embed ( string $source, string $site ) Outputs the generated embed code directly.

 


© Copyright 2024 lenbradley.dev - Chicago Area Web Developer