Update All Videos with Echove SDK

Technology
Media API
Required
Media API token, Echove PHP SDK
Edition
Pro, Enterprise

In this article we'll learn how to quickly update all of the videos in our account with a new tag. We'll be using Echove, an open-source PHP SDK for interacting with the Brightcove Media API.

If you haven't already downloaded Echove, you can get it at the Google Code page; documentation is available on the Echove website.

First we'll take a look at the entire code base, and then we'll go over it line-by-line to understand what's happening.

The Code

<?php

	# Include & Instantiate
	require('../echove.php');
	$bc = new Echove(
		'z9Jp-c3-KhWc4fqNf1JWz6SkLDlbO0m8UAwOjDBUSt0.',
		'z9Jp-c3-KhWc4fqNf1JWz6SkLDlbO0m8UAwOjDBUSt0.'
	);

	# Only return ID and Tags data
	$params = array(
		'video_fields' => 'id,tags'
	);
	
	# Request all videos from API
	$videos = $bc->find('allvideos', $params);
	
	# Loop through videos
	foreach($videos as $video)
	{
		# Assign tags to temporary variable
		$new_tags = $video->tags;
		
		# Add new tag
		$new_tags[] = 'newTag';
		
		# Create new meta data
		$new_meta = array(
			'id' => $video->id,
			'tags' => $new_tags
		);
		
		# Send changes to API
		$bc->update('video', $new_meta);
	}

?>

Explanation

In the first few lines, we're simply including the Echove SDK and instantiating it with our Media API Read and Write tokens.

Next we're putting together the parameters for our first API call and specifying that we only need to retrieve the ID and tags of each video (by limiting the fields returned, we can shave off some time). We then fire off a request for find_all_videos, and as you can see, it's a very simple process: we create a variable to hold the returned data, make a call to the find method of Echove, and then pass our desired API call and parameters.

Now we have all of our videos as an array inside of a variable and can easily loop through them. As we do, we assign the tags for each video to a temporary variable and add our new tag as well. From here, we can build the new metadata for the video; be sure to include the ID of the video and any updated fields. You don't have to pass all of the fields, just the modified ones.

We then make a quick call to the update method of Echove, passing in the type of item we want to update ('video') and the new metadata for that item. Publish the script to your server, run it, and in a few seconds all of the videos in your account will now have the new tag.