Order Now AdSolution Sign Up | Login » Bits on the Run Sign Up | Login »

Bits on the Run

Update Video Properties in PHP

When using the Bits on the Run API, it is fairly straightforward to update the properties (title, description, date, tags, etc.,) of a video. This is useful if you have your own CMS, but want to use the Bits on the Run database for storing the video properties. The more properties are set, the better you will be able to search, sort and select your videos when your library grows bigger.

Demo

Here's a working demo that updates the properties of a video. Its PHP code pulls the current properties from the API and displays then in the form. When the form is submitted, the PHP code sends the updated properties to the API:

The full source code of this demo can be downloaded as part of the PHP API Examples ZIP. The next section of this tutorial re-creates this code.

Setup

Since we have a handy API Kit available for PHP (also for .NET, Python), we will make use of that. The kit will take care of serializing and sending all data to the API, which makes development a lot easier. Make sure you have pasted the kit to a botr/ folder and then initialize it:

# Replace xxxx and yyyy with your API key/secret (can be found in the CMS).
require_once('botr/api.php');
$botr_api = new BotrAPI('xxxx','yyyy');

Next, let's set the video whose properties should be displayed. Note that we hard code the video key here, but usually you will have received the key from a GET or POST request.

# Insert the key of the video to update.
$video_key = '6RCvPeUn';

With the key in place, we can do a /videos/show API call to retrieve the properties:

# Grab the current properties for this video, so it can be printed in the form.
$response = $botr_api->call("/videos/show",array('video_key'=>$video_key));
if ($response['status'] == "error") { die(print_r($response)); }

Now we can print the HTML form and insert the properties:

<form method="post" action="./">
<fieldset>
    <input type="hidden" name="video_key" value="<?=$response['video']['key']?>" />
    <label>Title</label>
    <input type="text" name="title" value="<?=$response['video']['title']?>" />
    <label>Tags</label>
    <input type="text" name="tags" value="<?=$response['video']['tags']?>" />
    <label>Description</label>
    <textarea name="description"><?=$response['video']['description']?></textarea>
    <label>Author</label>
    <input type="text" name="author" value="<?=$response['video']['author']?>" />
    <label>Date</label>
    <input type="text" name="date" id="dateField" value="<?=date('m/d/Y',$response['video']['date'])?>" />
    <label>Link</label>
    <input type="text" name="link" value="<?=$response['video']['link']?>" />
</fieldset>
<fieldset>
    <button type="submit">save changes</button>
</fieldset>
</form>

When submitting this form, the current page reloads. Therefore, we should add another block of PHP code below the API initialization but above the /video/show/ call. This code will do a /videos/update API call In a more thorough setup, you might want to put this block on a separate page that then redirects to the form when done.

# If the form has been submitted, we place all properties in an array and save it.
# We only push properties that have a value, and we convert the date to a timestamp.
if(isset($_POST['video_key'])) {
    $array = array();
    foreach ($_POST as $key => $value) {
        if($key == 'date') {
            $date = explode('/',$value);
            $array[$key] = mktime(0,0,0,$date[0],$date[1],$date[2]);
        } else if($value) { 
            $array[$key] = $value; 
        }
    }
    $response = $botr_api->call("/videos/update",$array);
    if ($response['status'] == "error") { die(print_r($response)); }
}

That's all. You now have a simple PHP page that displays the properties of a video ánd saves them when the form is submitted. Again, the full PHP page can be downloaded as part of the PHP API Examples zip.

Enhancing the Update Form

A couple of steps can be taken to enhance this update form: