Updating All Videos using the PHP Media API Wrapper

In this topic you will learn how to quickly update all of the videos in your account with a new tag. We'll be using the PHP Media API Wrapper, an open-source PHP SDK for interacting with the Video Cloud Media API.

If you haven't already downloaded the PHP Media API Wrapper, you can get it at the opensource.brightcove.com site; documentation and sample code is also available there.

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('bc-mapi.php');
	$bc = new BCMAPI(
		'jskS1rEtQHy9exQKoc14IcMq8v5x2gCP6yaB7d0hraRtO__6HUuxMg..',
		'uikR5D2s7F-ODnXFRV6u7riQIDiHtsDNXRWsLS_mHltMKRMWr6mmYg..'
	);

	# Only return ID and Tags data
	$params = array(
		'video_fields' => 'id,tags'
	);
	
	# Request all videos from API
	$videos = $bc->findAll('video', $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 PHP Media API Wrapper and instantiating it with our Media API Read and Write tokens. To use this code yourself, you will need to substitute your own 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 findAll method of the PHP Media API Wrapper, 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 the PHP Media API Wrapper, 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.

Post new comment

The content of this field is kept private and will not be shown publicly.
0

Comments