Media Write API: PHP Example - Upload Video

Product
Video Cloud
Applies to Roles
Developer
Version
Brightcove 5
Modules
Media API
Edition
Pro, Enterprise

In this code walkthrough of an example application, you will learn how to use a PHP page to upload a video to your media library using PHP and the Media Write API. The video submission occurs on an HTML form. Then a PHP script handles the form and uploads the video to the Video Cloud service.

By following the code described in this topic, you will be able to build your own applications for uploading video or modify the example application as needed.

Before you begin

The sample consists of two files. Download these two files before you start:

  • Download uploadvideo.php, which is a PHP script that handles the form and uploads the video.
  • Download uploadVideoForm.html, which displays a form that the user uses to specify a video's name, description, tags, and path. The form's submit button passes the input to the PHP script.

Note: This code example is based on a predecessor of the PHP MAPI Wrapper project. For that PHP SDK, see http://opensource.brightcove.com/project/PHP-MAPI-Wrapper/. You'll also find an upload example under "Create - Video."

As well, ensure that you follow these prerequisites:

  • You should be familiar with PHP, JSON and cURL.
  • Ensure that the video file you are going to upload does not exceed the maximum file upload size limit of 2 GB.

In this topic, you will learn about:

Providing your tokens

To begin using the script, you will need to first provide valid tokens for your account. This block of code creates a new instance of the Brightcove class that is defined below. To learn more about how to access your tokens, read Managing Media API Tokens.

// Instantiate the Brightcove class
$bc = new Brightcove(
    '[[READ_TOKEN]]',
    '[[WRITE_TOKEN]]'
);

Handling the form submission

PHP makes it very easy to access the variables and files submitted with a form. The first part of the script parses the form submission and handles the file that the user specified:

// Set the data for the new video DTO using the form values
$metaData = array(
	'name' => $_POST['bcName'],
	'shortDescription' => $_POST['bcShortDescription']
);

// Rename the file to its original file name (instead of temp names like "a445ertd3")
$file = $_FILES['bcVideo'];
rename($file['tmp_name'], '/tmp/' . $file['name']);
$file = '/tmp/' . $file['name'];

Making the request

Now that you have gathered the metadata about your video and the file itself, you can call the createVideo method by passing it a reference to the file and that metadata.

// Send the file to Brightcove
echo $bc->createVideo($file, $metaData);

Understanding the Brightcove class

The Brightcove class performs the heavy lifting in this application. The Brightcove class is responsible for:

  • Gathering the proper variables required to make the request
  • Handling the HTTP request
  • Being able to properly retry the request if necessary

See these tasks in the code for the Brightcove class:

class Brightcove {
        public $token_read = '';
        public $token_write = '';
        public $read_url = 'http://api.brightcove.com/services/library?';
        public $write_url = 'http://api.brightcove.com/services/post';

	public function __construct($token_read, $token_write = NULL ) {
		$this->token_read = $token_read;
		$this->token_write = $token_write;
	}

	public function createVideo($file = NULL, $meta) {
		$request = array();
		$post = array();
		$params = array();
		$video = array();

		foreach($meta as $key => $value) {
			$video[$key] = $value;
		}
		$params['token'] = $this->token_write;
		$params['video'] = $video;

		$post['method'] = 'create_video';
		$post['params'] = $params;

		$request['json'] = json_encode($post);

		if($file) {
			$request['file'] = '@' . $file;
		}

		// Utilize CURL library to handle HTTP request
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $this->write_url);
		curl_setopt($curl, CURLOPT_POST, 1);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_VERBOSE, TRUE );
		curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 300);
		curl_setopt($curl, CURLOPT_TIMEOUT, 300);
		$response = curl_exec($curl);

		curl_close($curl);

		// Responses are transfered in JSON, decode into PHP object
		$json = json_decode($response);

		// Check request error code and re-call createVideo if request
                // returned a 213 error. A 213 error occurs when you have 
                // exceeded your allowed number of concurrent write requests
		if(isset($json->error))	{
			if($json->error->code == 213) {
				return $this->createVideo($file, $meta);
			} else {
				return FALSE;
			}
		} else {
			return $response;
		}
	}
}

Uploading using an HTML form

Now that you've defined the PHP script, you can put it to use in a simple HTML form. See the working example.

This sample defines a POST action for the script. The form also displays two input fields where a you can specify the name and description of the video that you're uploading, and a third input field where a you can browse for the video file in your file system:

<form method="post" action="uploadVideo.php" enctype="multipart/form-data">
	<input type="hidden" name="MAX_FILE_SIZE" value="16777000" />
	Title: <input type="text" name="bcName" /><br />
	Short Description: <textarea name="bcShortDescription"></textarea><br />
	File: <input type="file" name="bcVideo" /><br />
	<input type="submit" />
</form>

The form's Submit button passes the form input to the PHP script, which, as we've seen, parses the input and sets up and calls the createVideo method.

Tags
sample