Media Write API: PHP Example - Upload Video

Applies to Roles
Developer
Version
Brightcove 4
Modules
Media API
Edition
Pro, Enterprise

This example application uses 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, and this PHP script handles the form and uploads the video to the service. The sample consists of two files:

  • uploadvideo.php, the PHP script that handles the form and uploads the video.
  • uploadVideoForm.html, which presents a form in which the user enters a video's name, description, tags, and path. The form's submit button passes the input to the PHP script.

Before you begin

  • You should be familiar with PHP and cURL.
  • Make sure that the video file you are going to upload does not exceed the maximum file upload size limit. By default, this is 2 GB. You need either to change the maximum file upload size for PHP or upload a file that is under your current maximum upload size limit.
  • This example requires an existing directory named uploads in the directory where you will be placing the uploadvideo.php file. The uploads directory needs to have its write permissions set so that the web server and the world can write to it.

Handling the form submission

PHP makes it very easy to access the variables and files submitted with a form. The first steps in our script involve parsing the form submission and handling the file that the user specified:

// Turn on error reporting during development
ini_set('error_reporting', E_ALL);
ini_set('display_errors', TRUE);
 
// Create Constants
define("UPLOAD_CALL", "http://api.brightcove.com/services/post");
define("TOKEN", "GwrdB2odqtcmCsmU9UtmcxMX6GmQxydWMyhtGDezpNo.");
define("METHOD", "create_video");
 
 
// Create arrays
$post = array();
$params = array();
$video = array();
 
 
// Handle the uploaded file
// It needs to be moved out of the temp directory so PHP can
// determine the mime-type from the file extension which is 
// ".tmp" in the temp directory.
$file = $_FILES['file'];
$newFile = "./uploads/" . $file["name"];
if(file_exists($newFile)) { // If file already exists in the uploads folder
			  unlink($newFile);  // delete it before copying the new file
}
$result = move_uploaded_file($file["tmp_name"], $newFile);
 
 
// Extract the other metadata from the POST and put into vars
$video["name"] = $_POST["name"];
$video["shortDescription"] = $_POST["desc"];
$video["tags"] = explode(",",$_POST["tags"]);
 
$params["token"] = TOKEN;
$params["video"] = $video;
 
$post["method"] = METHOD;
$post["params"] = $params;

We've created UPLOAD_CALL, TOKEN, and METHOD variables to hold the parameters of our API call. These mirror the structure of the create_video method in the Media Write API. There's the overall post object, which contains the method name and the parameters. Parameters contains the token, and an object of the video metadata we're providing.

When moving the temp file, you'll need to take into account the file system which is serving this PHP script.

Assembling the request

Now that we have our post object, we can put the request together. PHP includes a native JSON library so it's very straightforward to JSON encode our post object:

// Assemble the multipart request: encode JSON part and the file part
$requestData["json"] = json_encode($post) . "\n";
$requestData["file"] = "@".dirname(__FILE__) . '/' . $newFile;

The "@" sign tells cURL to use the file, not just the string.

The Call

This sample uses uses native PHP functions, including the cURL (client URL) HTTP stack, which makes it easy to send and receive HTTP requests. Let's look at the structure of the call we'll use to upload our video to our Brightcove account:

// Execute the call using curl
$ch = curl_init(UPLOAD_CALL);
 
// Request type is post
curl_setopt($ch, CURLOPT_POST, true);
 
// Declare the post data that we assembled earlier
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestData);
 
// Return the result as a string of the return value of curl_exec instead of outputting it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
// Go!
$http_result = curl_exec($ch);
 
// Print the results if successful, otherwise print the errors :-(
if ($http_result) {
	echo "$http_result";
} else {
	echo curl_error($ch); 
	echo curl_getinfo($ch, CURLINFO_HEADER_OUT);
	echo curl_errno($ch); 
}

cURL lets you specify "options" that will affect the behavior of the request. Here are the important parameters:

  • POST declares that we're making a POST request.
  • POSTFIELDS is the data going in the body of the request.

Uploading using an HTML form

Now that we've defined our PHP Script , we can put it to use in a simple HTML form. See the sample.

In this sample, we define a POST action for our script. The form also presents two input fields where you can specify the name and description of the video you're uploading, and a third input field where you can browse for the video file in your file system:

<FORM ENCTYPE="multipart/form-data" method="POST" action="uploadVideo.php">
       Video name: <INPUT TYPE="text" NAME="name" value="video name"/>
       Description: <INPUT TYPE="text" NAME="desc" value="description"/>
       Tags: <INPUT TYPE="text" NAME="tags" value="foo,bar"/>
       File: <INPUT TYPE="file" NAME="file" />
     <INPUT TYPE="submit" VALUE="Upload"/>
</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 create_video method.

Tags
sample