Documentation / Code Examples

The Echove code examples provide further insight into the use of Echove methods.

The Echove PHP library contains four primary sections for helping you build your application. Core methods directly interact with the Echove class, Read methods correspond to Brightcove API read methods, Write methods correspond to Brightcove API write methods, and Convenience methods are available to help you convert returned data into a useful type for PHP.

Core Examples

Instantiation

Summary

This example shows how to instantiate, or start, the Echove PHP class. The first token, which is for the Read API, is required. The second token is for the Write API and is optional.

Example

<?php
	
	# Include the Echove SDK
	require('echove.php');
	
	# Instantiate the class, passing it our Brightcove API tokens
	$bc = new Echove(
		'z9Jp-c3-KhWc4fqNf1JWz6SkLDlbO0m8UAwOjDBUSt0.',
		'z9Jp-c3-KhUSoOh0IV8SA6ghleZlti6W9mmC_IhdCnybDfHqhmloA..'
	);
		
?>

Properties

Summary

This example shows how to set and retrieve some of the Echove properties that can be used for debugging and additional settings.

Example

<?php
	
	# Turn on HTTPS mode
	$bc->__set('secure', TRUE);
	
	# Make our API call
	$videos = $bc->find('allVideos');
	
	# Determine how many possible results there are
	echo 'Total Videos: ' . $bc->total_count . '<br />';

	# Make our API call
	$videos = $bc->findAll();
	
	# Determine how many times we called the Brightcove API
	echo 'API Calls: ' . $bc->__get('api_calls');
		
?>

Error Handling

Summary

This example shows how to utilize the built-in error handling in Echove.

Example

<?php
	
	# Create a try/catch
	try {
		# Make our API call
		$video = $bc->find('find_video_by_id', 8604819001);
	} catch(Exception $error) {
		# Handle our error
		echo $error;
		die();
	}
		
?>

Read Examples

Find Query

Summary

This example shows how to retrieve a video from a Brightcove account.

Example

<?php
	
	# Make our API call
	$video = $bc->find('find_video_by_id', 8604819001);

	# Print the video name and ID
	echo $video->name . ' (' . $video->id . ')';
		
?>

Find Query - Shorthand

Summary

This example shows how you can use shorthand method names to make code easier to write and read.

Example

<?php
	
	# Make our API call
	$video = $bc->find('videoById', 8604819001);

?>

Find Query - Additional Parameters

Summary

This example shows how to define additional API call parameters using a key-value array.

Example

<?php
	
	# Define our parameters
	$params = array(
		'id' => 8604819001,
		'video_fields' => 'video_id,name,shortDescription'
	);
	
	# Make our API call
	$video = $bc->find('videoById', $params);
		
?>

Find Query - True Find All

Summary

Brightcove limits the "find_all_videos" call to 100 results, requiring pagination and numerous API calls. This example shows how to use the findAll() method to do this automatically.

WARNING: Use very carefully

Example

<?php
	
	# Define our parameters
	$params = array(
		'video_fields' => 'id,name'
	);
	
	# Make our API call
	$videos = $bc->findAll('video', $params);
		
?>

Write Examples

Create - Video

Summary

This example details how to upload a video to a Brightcove account. This code is handling data that was passed from a form. Note that we re-name the uploaded movie to its original name rather than the random string generated when it's placed in the "tmp" directory; this is because the tmp_name does not include the file extension. The video name is a required field.

Example

<?php
			
	# Create an array of meta data from our form fields
	$metaData = array(
		'name' => $_POST['videoName'],
		'shortDescription' => $_POST['videoShortDescription']
	);

	# Move the file out of 'tmp', or rename
	rename($_FILES['videoFile']['tmp_name'], '/tmp/' . $_FILES['videoFile']['name']);
	$file = '/tmp/' . $_FILES['videoFile']['name'];
	
	# Upload the video and save the video ID
	$id = $bc->createMedia('video', $file, $metaData);

?>

Create - Image

Summary

This example details how to upload a image to a Brightcove account. This code is handling data that was passed from a form. Note that we re-name the uploaded image to its original name rather than the random string generated when it's placed in the "tmp" directory; this is because the tmp_name does not include the file extension.

Example

<?php
			
	# Create an array of meta data from our form fields
	$metaData = array(
		'type' => 'VIDEO_STILL',
		'displayName' => $_POST['imageName']
	);

	# Move the file out of 'tmp', or rename
	rename($_FILES['bcImage']['tmp_name'], '/tmp/' . $_FILES['bcImage']['name']);
	$file = '/tmp/' . $_FILES['bcImage']['name'];
	
	# Upload the image, assign to a video, and save the image asset ID
	$id = $bc->createImage('video', $file, $metaData, 8604819001);

?>

Create - Playlist

Summary

This example shows how to create a playlist in a Brightcove account. The code is handling data that was passed from a form. The name, video IDs, and playlist type are all required fields.

Example

<?php
			
	# Take a comma-separated string of video IDs and explode into an array
	$videoIds = explode(',', $_POST['playlistVideoIds']);
	
	# Create an array of meta data from our form fields
	$metaData = array(
		'name' => $_POST['playlistName'],
		'shortDescription' => $_POST['playlistShortDescription'],
		'videoIds' => $videoIds,
		'playlistType' => 'explicit'
	);
	
	# Create the playlist and save the playlist ID
	$id = $bc->createPlaylist('video', $metaData);

?>

Update - Video / Playlist

Summary

This example shows how to update a video, but the same method will work for a playlist.

Example

<?php
			
	# Create an array of the new meta data
	$metaData = array(
		'id' => 8604819001,
		'shortDescription' => 'Our new short description.'
	);
	
	# Update a video with the new meta data
	$bc->update('video', $metaData);

?>

Delete - Video / Playlist

Summary

This example shows how to delete a video, but the same method will work for a playlist. Cascaded deletion means that the video will also be removed from all playlists and players.

Example

<?php
			
	# Delete a 'video' by ID, and cascade the deletion
	$bc->delete('video', 8604819001, NULL, TRUE);

?>

Status - Video Upload

Summary

This example shows how to determine the status of a video being uploaded to a Brightcove account.

Example

<?php
	
	# Retrieve upload status		
	$status = $bc->getStatus('video', 8604819001);

?>

Share Video

Summary

This example shows how to share a video with another Brightcove account. A list of the new video IDs will be returned. Note that sharing must be enabled between the two accounts.

Example

<?php
	
	# List the accounts to share the video with
	$ids = array(
		123456789
	);
	
	# Share the videos, and save the new video IDs
	$new_ids = $bc->shareMedia('video', 8604819001, $ids);

?>

Add To / Remove From Playlist

Summary

This example shows how to add an asset to a playlist, as well as how to remove an asset from a playlist. You may pass an array of video IDs, or a single video ID.

Example

<?php
			
	# Add two videos to a playlist
	$bc->addToPlaylist(44969780001, array(45061414001, 45062831001));
	
	# Remove a video from a playlist
	$bc->removeFromPlaylist(44969780001, 45062831001);

?>

Convenience Examples

SEF URLs / Time Formatting

Summary

This example shows the Echove convenience methods that convert video titles into a search-engine friendly format and video lengths into formatted strings.

Example

<?php
	
	# Make our API call
	$video = $bc->find('videoById', 8604819001);
	
	# Print the SEF video name and formatted duration
	echo 'Name: ' . $bc->sef($video->name) . '<br />';
	echo 'Duration:' . $bc->time($video->length) . '<br />';
		
?>

Automatic Timestamp Conversion

Summary

To more seamlessly bridge the Brightcove API into PHP the 'from_date' parameter for the "find_modified_videos" call should be provided as seconds since Epoch (UNIX timestamp) instead of minutes since, as the Brightcove Media API documentation states. You can still pass minutes if you prefer.

Example

<?php
	
	# Set timestamp to 7 days ago (in seconds)
	$time = time() - 604800;
	
	# Make our API call
	$videos = $bc->find('modifiedVideos', $time);
	
	# Set timestamp to 7 days ago (in minutes)
	$time = floor((time() - 604800) / 60);
	
	# Make our API call
	$videos = $bc->find('modifiedVideos', $time);
		
?>

Tags

Summary

This example demonstrates how a tag with a value of "abc=xyz" can easily be parsed into a key-value array pair.

Example

<?php
	
	# Make our API call
	$video = $bc->find('videoById', 8604819001);
	
	# Parse any key=value tags into array
	$video->tags = $bc->tags($video->tags);
	
	# Print out each tag
	foreach($video->tags as $key => $value)
	{
		echo $key . ': ' . $value . '<br />';
	}
		
?>

Tag Filter

Summary

This example shows how to remove all videos that don't contain any of the listed tags.

Example

<?php
	
	# Make our API call
	$videos = $bc->find('allVideos');
	
	# Remove all videos without specified tags
	$videos = $bc->filter($videos, 'published=true,include=true');
		
?>

Embed Player - Simple

Summary

This example shows how to dynamically embed a Brightcove player with a single video.

Example

<script language="JavaScript" type="text/javascript" src="http://admin.brightcove.com/js/BrightcoveExperiences.js"></script>
<?php

	# Set our parameters
	$params = array(
		'width' => 312,
		'height' => 567
	);
	
	$playerId = 10506233001;
	$videoId = 10508262001;
	
	# Print the embed code
	echo $bc->embed('video', $playerId, $videoId, $params);

?>

Embed Player - Advanced

Summary

This example shows how to dynamically embed a Brightcove player with a video defined by reference ID and a transparent background.

Example

<script language="JavaScript" type="text/javascript" src="http://admin.brightcove.com/js/BrightcoveExperiences.js"></script>
<?php

	# Set our parameters
	$params = array(
		'width' => 312,
		'height' => 567
	);
	
	$playerId = 10506233001;
	
	# Set additional parameters
	$additional = array(
		'@videoPlayer' => 'ref:myVid12345',
		'wmode' => 'transparent'
	);
	
	# Print the embed code
	echo $bc->embed('video', $playerId, NULL, $params, $additional);

?>