Live API Sample: Create a Live Job

In this sample, you will learn how to create a simple Live job.

Introduction

This sample creates a simple Live job with three output renditions: 1080p, 720p, and 480p.

Because the Live API is not CORS-enabled and must be accessed from a server-side app, the API request is sent through a simple proxy written in PHP. You can reproduce this in any server-side language - all it does is collect the request parameters sent by the JavaScript, sends the request to the API, and returns the response to the JavaScript. All the code can be found in the code section below.

Create Live job app

Find all the code associated with this sample in this GitHub repository.

See the Pen Live API Sample: Create a Live Job by Brightcove Learning Services (@rcrooks1969) on CodePen.

Using the CodePen

Here are some tips to effectively use the above CodePen:

  • Toggle the actual display of the player by clicking the Result button.
  • Click the HTML/CSS/JS buttons to display ONE of the code types.
  • Click Edit on CodePen in the upper right corner to fork this CodePen into your own account.
  • Find all the code associated with this sample in this GitHub repository.

Proxy code

In addition to the code found in CodePen (and associated GitHub repo), this sample requires a proxy to make the API request and return the response to the app. For this app we used PHP, and the code is shown below. You can use any server-side language to build the proxy.

Proxy sample code
	<?php
	/**
	 * live-proxy.php - proxy for Brightcove Live APIs
	 * makes the request, and returns the response
	 * Accessing:
	 *    (note you should *always* access the proxy via HTTPS)
	 *    Method: POST
	 *
	 * @post {string} url - the URL for the API request
	 * @post {string} [requestType=GET] - HTTP method for the request
	 * @post {string} [requestBody] - JSON data to be sent with write requests
	 * @post {string} apiKey - Live API key
	 *
	 * @returns {string} $response - JSON response received from the API
	 */
	
	
	// CORS enablement and other headers
	header("Access-Control-Allow-Origin: *");
	header("Content-type: application/json");
	header("X-Content-Type-Options: nosniff");
	header("X-XSS-Protection");
	
	
	$requestData = json_decode(file_get_contents('php://input'));
	
	// set up the API call
	// get api key
	$apikey = $requestData->apiKey;
	// get request type or default to GET
	if ($requestData->requestType) {
			$method = $requestData->requestType;
	} else {
			$method = "GET";
	}
	// more security checks
	$needle = '.io';
	$endapi = strpos($requestData->url, $needle) + 3;
	
	$nextChar = substr($requestData->url, $endapi, 1);
	
	if (strpos($requestData->url, 'api.bcovlive.io') == false) {
			exit('{"ERROR":"Only requests to Brightcove Live APIs are accepted by this proxy"}');
	} else if ($nextChar !== '/' && $nextChar !== '?') {
			exit('{"ERROR": "There was a problem with your API request - please check the URL"}');
	}
	// get the URL and authorization info from the form data
	$request = $requestData->url;
	//send the http request
	if ($requestData->requestBody) {
		$ch = curl_init($request);
		curl_setopt_array($ch, array(
			CURLOPT_CUSTOMREQUEST  => $method,
			CURLOPT_RETURNTRANSFER => TRUE,
			CURLOPT_SSL_VERIFYPEER => FALSE,
			CURLOPT_HTTPHEADER     => array(
				'Content-type: application/json',
				"X-API-KEY: {$apikey}",
			),
			CURLOPT_POSTFIELDS => $requestData->requestBody
		));
		$response = curl_exec($ch);
		curl_close($ch);
	} else {
		$ch = curl_init($request);
		curl_setopt_array($ch, array(
			CURLOPT_CUSTOMREQUEST  => $method,
			CURLOPT_RETURNTRANSFER => TRUE,
			CURLOPT_SSL_VERIFYPEER => FALSE,
			CURLOPT_HTTPHEADER     => array(
				'Content-type: application/json',
				"X-API-KEY: {$apikey}",
			)
		));
		$response = curl_exec($ch);
		curl_close($ch);
	}
	
	// Check for errors
	if ($response === FALSE) {
			$logEntry = "\nError:\n".
			"\n".date("Y-m-d H:i:s")." UTC \n"
			.$response;
			$logFileLocation = "log.txt";
			$fileHandle      = fopen($logFileLocation, 'a') or die("-1");
			fwrite($fileHandle, $logEntry);
			fclose($fileHandle);
			echo '{"ERROR": "There was a problem with your API call"}'+
			die(curl_error($ch));
	}
	
	// Decode the response
	// $responseData = json_decode($response, TRUE);
	// return the response to the AJAX caller
	$responseDecoded = json_decode($response);
	// if (!isset($responseDecoded)) {
	// 	$response = '{null}';
	// }
	echo $response;
	?>