2017 © Pedro Peláez
 

library mapi

Brightcove Media API for PHP

image

brightcove/mapi

Brightcove Media API for PHP

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 53 Forks
  • 1 Open issues
  • 1 Versions
  • 3 % Grown

The README.md

About

This project provides a starting point for integrating the Brightcove Media API into your application. It provides simple ways to interact with the API, as well as a long list of helper functions., (*1)

Compatibility Notice

Please note that the PHP MAPI Wrapper v2.0 is not compatible with any previous versions (when it was known as "Echove"). The class name has been changed, numerous functions have been re-named, and methods have been updated to take advantage of Brightcove API changes., (*2)

If you need assistance in determining what changes have been made, please send an e-mail to opensource@brightcove.com with your request., (*3)

Requirements

PHP version 5.2 or greater, or you must have the JavaScript Object Notation (JSON) PECL package. For more information on the JSON PECL package, please visit the PHP JSON package website., (*4)

Using Cache Extension

The PHP MAPI Wrapper includes a caching extension. To use this feature, include the file on your page along with the core PHP MAPI Wrapper file., (*5)

require('bc-mapi.php');
require('bc-mapi-cache.php');

Then, after instantiating the core class, you can instantiate the caching extension., (*6)

// Using flat files
$bc = new BCMAPI(API_READ_TOKEN, API_WRITE_TOKEN);
$bc_cache = new BCMAPICache('file', 600, '/var/www/myWebSite/cache/', '.cache');

// Using Memcached
$bc = new BCMAPI(API_READ_TOKEN, API_WRITE_TOKEN);
$bc_cache = new BCMAPICache('memcached', 600, 'localhost', NULL, 11211);

The parameters for the constructor are:, (*7)

  • [string] The type of caching method to use, either 'file' or 'memcached'
  • [int] How many seconds until cache files are considered cold
  • [string] The absolute path of the cache directory (file) or host (memcached)
  • [string] The file extension for cache items (file only)
  • [int] The port to use (Memcached only)

Examples

Instantiation

This example shows how to instantiate, or start, the BCMAPI PHP class. The first token, which is for the Read API, is required. The second token is for the Write API and is optional., (*8)

// Include the BCMAPI SDK
require('bc-mapi.php');

// Instantiate the class, passing it our Brightcove API tokens (read, then write)
$bc = new BCMAPI(
    'READ_API_TOKEN',
    'WRITE_API_TOKEN'
);

// You may optionally include the caching extension provided with BCMAPI...
require('bc-mapi-cache.php');

// Using flat files
$bc_cache = new BCMAPICache('file', 600, '/var/www/myWebSite/cache/', '.cache');

// Using Memcached
$bc_cache = new BCMAPICache('memcached', 600, 'localhost', NULL, 11211);

Properties

This example shows how to set and retrieve some of the BCMAPI properties that can be used for debugging and additional settings., (*9)

// Turn on HTTPS mode
$bc->__set('secure', TRUE);

// Make our API call
$videos = $bc->find('allVideos');

// Determine how many possible results there are
echo 'Total Videos: ' . $bc->total_count . '<br />';

// Make our API call
$videos = $bc->findAll();

// Determine how many times we called the Brightcove API
echo 'API Calls: ' . $bc->__get('api_calls');

Regional Support (Internationalization)

This example shows how to change the API URLs for supporting international regions., (*10)

// Change our region to Japan
$bc->__set('url_read', 'api.brightcove.co.jp/services/library?');
$bc->__set('url_write', 'api.brightcove.co.jp/services/post');

Error Handling

This example shows how to utilize the built-in error handling in BCMAPI., (*11)

// Create a try/catch
try {
    // Make our API call
    $video = $bc->find('find_video_by_id', 123456789);
} catch(Exception $error) {
    // Handle our error
    echo $error;
    die();
}

Find Query

This example shows how to retrieve a video from a Brightcove account., (*12)

// Make our API call
$video = $bc->find('find_video_by_id', 123456789);

// Print the video name and ID
echo $video->name . ' (' . $video->id . ')';

Find Query - Shorthand

This example shows how you can use shorthand method names to make code easier to write and read., (*13)

// Make our API call
$video = $bc->find('videoById', 123456789);

Find Query - Additional Parameters

This example shows how to define additional API call parameters using a key-value array., (*14)

// Define our parameters
$params = array(
    'id' => 123456789,
    'video_fields' => 'video_id,name,shortDescription'
);

// Make our API call
$video = $bc->find('videoById', $params);

Find Query - True Find All

Brightcove limits the "find_all_videos" call to 100 results, requiring pagination and numerous API calls. This example shows how to use the findAll() method to do this automatically. WARNING: Use very carefully * // Define our parameters $params = array( 'video_fields' => 'id,name' );, (*15)

// Make our API call
$videos = $bc->findAll('video', $params);

Search Query

This example shows how to search for a video about "gates", but not "Bill Gates"., (*16)

// Define our parameters
$params = array(
    'video_fields' => 'id,name,shortDescription'
);

// Set our search terms
$terms = array(
    'all' => 'display_name:gates',
    'none' => 'display_name:bill'
);

// Make our API call
$videos = $bc->search('video', $terms, $params);

This example shows how to search for a video with "jobs" in the title AND tags., (*17)

// Define our parameters
$params = array(
    'video_fields' => 'id,name,shortDescription'
);

// Set our search terms
$terms = array(
    'all' => 'display_name:jobs,tag:jobs'
);

// Make our API call
$videos = $bc->search('video', $terms, $params);

Create - Video

This example details how to upload a video to a Brightcove account. This code is handling data that was passed from a form. Note that we re-name the uploaded movie to its original name rather than the random string generated when it's placed in the "tmp" directory; this is because the tmp_name does not include the file extension. The video name is a required field., (*18)

// Create an array of meta data from our form fields
$metaData = array(
    'name' => $_POST['videoName'],
    'shortDescription' => $_POST['videoShortDescription']
);

// Move the file out of 'tmp', or rename
rename($_FILES['videoFile']['tmp_name'], '/tmp/' . $_FILES['videoFile']['name']);
$file = '/tmp/' . $_FILES['videoFile']['name'];

// Upload the video and save the video ID
$id = $bc->createMedia('video', $file, $metaData);

Create - Image

This example details how to upload a image to a Brightcove account. This code is handling data that was passed from a form. Note that we re-name the uploaded image to its original name rather than the random string generated when it's placed in the "tmp" directory; this is because the tmp_name does not include the file extension., (*19)

// Create an array of meta data from our form fields
$metaData = array(
    'type' => 'VIDEO_STILL',
    'displayName' => $_POST['imageName']
);

// Move the file out of 'tmp', or rename
rename($_FILES['bcImage']['tmp_name'], '/tmp/' . $_FILES['bcImage']['name']);
$file = '/tmp/' . $_FILES['bcImage']['name'];

// Upload the image, assign to a video, and save the image asset ID
$id = $bc->createImage('video', $file, $metaData, 123456789);

Create - Playlist

This example shows how to create a playlist in a Brightcove account. The code is handling data that was passed from a form. The name, video IDs, and playlist type are all required fields., (*20)

// Take a comma-separated string of video IDs and explode into an array
$videoIds = explode(',', $_POST['playlistVideoIds']);

// Create an array of meta data from our form fields
$metaData = array(
    'name' => $_POST['playlistName'],
    'shortDescription' => $_POST['playlistShortDescription'],
    'videoIds' => $videoIds,
    'playlistType' => 'explicit'
);

// Create the playlist and save the playlist ID
$id = $bc->createPlaylist('video', $metaData);

Update - Video / Playlist

This example shows how to update a video, but the same method will work for a playlist., (*21)

// Create an array of the new meta data
$metaData = array(
    'id' => 123456789,
    'shortDescription' => 'Our new short description.'
);

// Update a video with the new meta data
$bc->update('video', $metaData);

Delete - Video / Playlist

This example shows how to delete a video, but the same method will work for a playlist. Cascaded deletion means that the video will also be removed from all playlists and players., (*22)

// Delete a 'video' by ID, and cascade the deletion
$bc->delete('video', 123456789, NULL, TRUE);

Status - Video Upload

This example shows how to determine the status of a video being uploaded to a Brightcove account., (*23)

// Retrieve upload status
$status = $bc->getStatus('video', 123456789);

Share Video

This example shows how to share a video with another Brightcove account. A list of the new video IDs will be returned. Note that sharing must be enabled between the two accounts., (*24)

// List the accounts to share the video with
$ids = array(
    123456789
);

// Share the videos, and save the new video IDs
$new_ids = $bc->shareMedia('video', 123456789, $ids);

Add To / Remove From Playlist

This example shows how to add an asset to a playlist, as well as how to remove an asset from a playlist. You may pass an array of video IDs, or a single video ID., (*25)

// Add two videos to a playlist
$bc->addToPlaylist(555555555, array(123456789, 987654321));

// Remove a video from a playlist
$bc->removeFromPlaylist(555555555, 987654321);

SEF URLs / Time Formatting

This example shows the BCMAPI convenience methods that convert video titles into a search-engine friendly format and video lengths into formatted strings., (*26)

// Make our API call
$video = $bc->find('videoById', 123456789);

// Print the SEF video name and formatted duration
echo 'Name: ' . $bc->sef($video->name) . '<br />';
echo 'Duration:' . $bc->time($video->length) . '<br />';

Automatic Timestamp Conversion

To more seamlessly bridge the Brightcove API into PHP the 'from_date' parameter for the "find_modified_videos" call should be provided as seconds since Epoch (UNIX timestamp) instead of minutes since, as the Brightcove Media API documentation states. You can still pass minutes if you prefer., (*27)

// Set timestamp to 7 days ago (in seconds)
$time = time() - 604800;

// Make our API call
$videos = $bc->find('modifiedVideos', $time);

// Set timestamp to 7 days ago (in minutes)
$time = floor((time() - 604800) / 60);

// Make our API call
$videos = $bc->find('modifiedVideos', $time);

Tags

This example demonstrates how a tag with a value of "abc=xyz" can easily be parsed into a key-value array pair., (*28)

// Make our API call
$video = $bc->find('videoById', 123456789);

// Parse any key=value tags into array
$video->tags = $bc->tags($video->tags);

// Print out each tag
foreach($video->tags as $key => $value)
{
echo $key . ': ' . $value . '<br />';
}

Tag Filter

This example shows how to remove all videos that don't contain any of the listed tags., (*29)

// Make our API call
$videos = $bc->find('allVideos');

// Remove all videos without specified tags
$videos = $bc->filter($videos, 'published=true,include=true');

Methods

BCMAPI

The constructor for the BCMAPI class., (*30)

Arguments

  • token_read The read API token for the Brightcove account, (*31)

    Default: NULL Type: String, (*32)

  • token_write The write API token for the Brightcove account, (*33)

    Default: NULL Type: String, (*34)

Properties

  • api_calls Private - The total number of API calls that have been processed, (*35)

    Type: Integer, (*36)

  • media_delivery Private - What type of URL to return for UDS assets, (*37)

    Type: String, (*38)

  • page_number Public - The value of the last 'page_number' return, (*39)

    Type: Integer, (*40)

  • page_size Public - The value of the last 'page_size' return, (*41)

    Type: Integer, (*42)

  • secure Private - Whether BCMAPI is operating over HTTPS, (*43)

    Type: Boolean, (*44)

  • show_notices Private - Whether BCMAPI will send error notices, (*45)

    Type: Boolean, (*46)

  • timeout_attempts Private - The number of times to retry a call in case of API timeout, (*47)

    Type: Integer, (*48)

  • timeout_delay Private - Number of seconds to delay retry attempts, (*49)

    Type: Integer, (*50)

  • timeout_retry Private - Whether to automatically retry calls that fail due to API timeout, (*51)

    Type: Boolean, (*52)

  • token_read Private - The read Brightcove token to use, (*53)

    Type: String, (*54)

  • token_write Private - The write Brightcove token to use, (*55)

    Type: String, (*56)

  • total_count Public - The value of the last 'total_count' return, (*57)

    Type: Integer, (*58)

__set

Sets a property of the BCMAPI class., (*59)

Arguments

  • key The property to set, (*60)

    Type: String, (*61)

  • value The new value for the property, (*62)

    Type: Mixed, (*63)

Return Value

The new value of the property, (*64)

Type:       Mixed

__get

Retrieves a property of the BCMAPI class., (*65)

Arguments

  • key The property to retrieve, (*66)

    Type: String, (*67)

Return Value

The value of the property, (*68)

Type:       Mixed

find

Formats the request for any API "Find" methods and retrieves the data. The requested call may be written in a shortened version (e.g. "allVideos" or "all_videos" instead of "find_all_videos"). If the call supports get_item_count, it is defaulted to TRUE., (*69)

Arguments

  • call The requested API method, (*70)

    Type: String, (*71)

  • params A key-value array of API parameters, or a single value that matches the default, (*72)

    Default: NULL Type: Mixed, (*73)

Return Value

An object containing all API return data, (*74)

Type:       Object

findAll

Finds all media assets in account, ignoring pagination. This method should be used with extreme care as accounts with a large library of assets will require a high number of API calls. This could significantly affect performance and may result in additional charges from Brightcove., (*75)

Arguments

  • type The type of object to retrieve, (*76)

    Default: video Type: String, (*77)

  • params A key-value array of API parameters, (*78)

    Default: NULL Type: Array, (*79)

Return Value

An object containing all API return data, (*80)

Type:       Object

Performs a search of video meta data, (*81)

Arguments

  • type The type of objects to retrieve, (*82)

    Default: video Type: String, (*83)

  • terms The terms to use for the search, (*84)

    Default: NULL Type: Array, (*85)

  • params A key-value array of API parameters, (*86)

    Default: NULL Type: Mixed, (*87)

Return Value

An object containing all API return data, (*88)

Type:       Object

createMedia

Uploads a media asset file to Brightcove. When creating an asset from an upload it is suggested that you first move the file out of the temporary directory where PHP placed it and rename the file to it's original name. An asset name and short description are both required; leaving these values blank will cause them to be populated with the current UNIX timestamp. Certain upload settings are not allowed depending upon what default have already been set, and depending on the type of file being uploaded. Setting the incorrect values for these parameters will trigger a notice., (*89)

Arguments

  • type The type of object to upload, (*90)

    Default: video Type: String, (*91)

  • file The location of the temporary file, (*92)

    Default: NULL Type: String, (*93)

  • meta The media asset information, (*94)

    Type: Array, (*95)

  • options Optional upload values, (*96)

    Default: NULL Type: Array, (*97)

Return Value

The media asset ID, (*98)

Type:       String

createPlaylist

Creates a playlist., (*99)

Arguments

  • type The type of playlist to create, (*100)

    Default: video Type: String, (*101)

  • meta The playlist information, (*102)

    Type: Array, (*103)

Return Value

The playlist ID, (*104)

Type:       String

update

Updates a media asset. Only the meta data that has changed needs to be passed along. Be sure to include the asset ID, though., (*105)

Arguments

  • type The type of object to update, (*106)

    Default: video Type: String, (*107)

  • meta The information for the media asset, (*108)

    Type: Array, (*109)

Return Value

The new DTO, (*110)

Type:       DTO

createImage

Uploads a media image file to Brightcove. When creating an image it is suggested that you first move the file out of the temporary directory where PHP placed it and rename the file to it's original name., (*111)

Arguments

  • type The type of object to upload image for, (*112)

    Default: video Type: String, (*113)

  • file The location of the temporary file, (*114)

    Default: NULL Type: String, (*115)

  • meta The image information, (*116)

    Type: Array, (*117)

  • id The ID of the media asset to assign the image to, (*118)

    Default: NULL Type: Integer, (*119)

  • ref_id The reference ID of the media asset to assign the image to, (*120)

    Default: NULL Type: String, (*121)

  • resize Whether or not to resize the image on upload, (*122)

    Default: TRUE Type: Boolean, (*123)

Return Value

The image asset, (*124)

Type:       Mixed

createOverlay

Uploads a logo overlay file to Brightcove. When creating a logo overlay it is suggested that you first move the file out of the temporary directory where PHP placed it and rename the file to it's original name., (*125)

Arguments

  • file The location of the temporary file, (*126)

    Default: NULL Type: String, (*127)

  • meta The logo overlay information, (*128)

    Type: Array, (*129)

  • id The ID of the media asset to assign the logo overlay to, (*130)

    Default: NULL Type: Integer, (*131)

  • ref_id The reference ID of the media asset to assign the logo overlay to, (*132)

    Default: NULL Type: String, (*133)

Return Value

The logo overlay asset, (*134)

Type:       Mixed

deleteOverlay

Deletes a logo overlay., (*135)

Arguments

  • id The ID of the media asset, (*136)

    Default: NULL Type: Integer, (*137)

  • ref_id The reference ID of the media asset, (*138)

    Default: NULL Type: String, (*139)

  • options Optional values, (*140)

    Default: NULL Type: Array, (*141)

delete

Deletes a media asset. Either an ID or Reference ID must be passed., (*142)

Arguments

  • type The type of the item to delete, (*143)

    Default: video Type: String, (*144)

  • id The ID of the media asset, (*145)

    Default: NULL Type: Integer, (*146)

  • ref_id The reference ID of the media asset, (*147)

    Default: NULL Type: String, (*148)

  • options Optional values, (*149)

    Default: NULL Type: Array, (*150)

getStatus

Retrieves the status of a media asset upload., (*151)

Arguments

  • type The type of object to check, (*152)

    Default: video Type: String, (*153)

  • id The ID of the media asset, (*154)

    Default: NULL Type: String, (*155)

  • ref_id The reference ID of the media asset, (*156)

    Default: TRUE Type: String, (*157)

Return Value

The upload status, (*158)

Type:       String

shareMedia

Shares a media asset with the selected accounts. Sharing must be enabled between the two accounts., (*159)

Arguments

  • type The type of object to share, (*160)

    Default: video Type: String, (*161)

  • id The ID of the media asset, (*162)

    Type: Integer, (*163)

  • account_ids An array of account IDs, (*164)

    Type: Array, (*165)

  • accept Whether the share should be auto accepted, (*166)

    Default: FALSE Type: Boolean, (*167)

  • force Whether the share should overwrite existing copies of the media, (*168)

    Default: FALSE Type: Boolean, (*169)

Return Value

The new media asset IDs, (*170)

Type:       Array

removeFromPlaylist

Removes assets from a playlist., (*171)

Arguments

  • playlist_id The ID of the playlist to modify, (*172)

    Type: Integer, (*173)

  • video_ids An array of video IDs to delete from the playlist, (*174)

    Type: Array, (*175)

Return Value

The new playlist DTO, (*176)

Type:       Array

addToPlaylist

Adds assets to a playlist., (*177)

Arguments

  • playlist_id The ID of the playlist to modify, (*178)

    Type: Integer, (*179)

  • video_ids An array of video IDs to add to the playlist, (*180)

    Type: Array, (*181)

Return Value

The new playlist DTO, (*182)

Type:       Array

convertTime

Converts milliseconds to formatted time or seconds., (*183)

Arguments

  • ms The length of the media asset in milliseconds, (*184)

    Default:
    Type: Integer, (*185)

  • seconds Whether to return only seconds, (*186)

    Default: FALSE Type: Boolean, (*187)

Return Value

The formatted length or total seconds of the media asset, (*188)

Type:       Mixed

convertTags

Parses media asset tags array into a key-value array., (*189)

Arguments

  • tags The tags array from a media asset DTO, (*190)

    Default:
    Type: Array, (*191)

  • implode Return array to Brightcove format, (*192)

    Default: FALSE Type: Boolean, (*193)

Return Value

A key-value array of tags, or a comma-separated string, (*194)

Type:       Mixed

tagsFilter

Removes assets that don't contain the appropriate tags., (*195)

Arguments

  • videos All the assets you wish to filter, (*196)

    Default:
    Type: Array, (*197)

  • tag A comma-separated list of tags to filter on, (*198)

    Default:
    Type: String, (*199)

Return Value

The filtered list of assets, (*200)

Type:       Array

sef

Formats a media asset name to be search-engine friendly., (*201)

Arguments

  • name The asset name, (*202)

    Default:
    Type: String, (*203)

Return Value

The search-engine friendly asset name, (*204)

Type:       String

BCMAPICache

The constructor for the BCMAPICache class., (*205)

Arguments

  • type The type of caching method to use, either 'file' or 'memcached', (*206)

    Default: file Type: String, (*207)

  • time How many seconds until cache files are considered cold, (*208)

    Default: 600 Type: Integer, (*209)

  • location The absolute path of the cache directory (file) or host (memcached), (*210)

    Default:
    Type: String, (*211)

  • extension The file extension for cache items (file only), (*212)

    Default: .c Type: String, (*213)

  • port, (*214)

    Default: 11211 Type: Integer, (*215)

Properties

  • extension Public - The file extension for cache items (file only), (*216)

    Type: String, (*217)

  • location Public - The absolute path of the cache directory (file) or host (memcached), (*218)

    Type: String, (*219)

  • memcached Public - The Memcached object, if valid, (*220)

    Type: Object, (*221)

  • port Public - The port to use (Memcached only), (*222)

    Type: Integer, (*223)

  • time Public - How many seconds until cache files are considered cold, (*224)

    Type: Integer, (*225)

  • type Public - The type of caching method to use, either 'file' or 'memcached', (*226)

    Type: String, (*227)


Errors

BCMAPIApiError

This is the most generic error returned from BCMAPI as it is thrown whenever the API returns unexpected data, or an error. The API return data will be included in the error to help you diagnose the problem., (*228)

BCMAPIDeprecated

The requested item is no longer supported by Brightcove and/or BCMAPI. Stop using this method as early as possible, as the item could be removed in any future release., (*229)

BCMAPIDtoDoesNotExist

The specified asset does not exist in the Brightcove system. Ensure you're using the correct ID., (*230)

BCMAPIIdNotProvided

An ID has not been passed to the method (usually a "delete" or "share" function). Include the ID parameter to resolve the error., (*231)

BCMAPIInvalidFileType

The file being passed to the function is not supported. Try another file type to resolve the error., (*232)

BCMAPIInvalidMethod

The "find" method being requested is not supported by BCMAPI, or does not exist in the Brightcove API. Remove the method call and check both the BCMAPI and Brightcove API documentation., (*233)

BCMAPIInvalidProperty

The BCMAPI property you are trying to set or retrieve does not exist. Check the BCMAPI documentation., (*234)

BCMAPIInvalidType

The DTO type (video, playlist, image, etc) you specified is not allowed for the method. Check both the BCMAPI and Brightcove API documentation., (*235)

BCMAPISearchTermsNotProvided

Please specify one or more search parameters. Verify you are passing the parameters in an array., (*236)

BCMAPITokenError

The read or write token you provided is not recognized by Brightcove. Verify you are using the correct token., (*237)

BCMAPITransactionError

The API could not be accessed, or the API did not return any data. Verify the server has cURL installed, enabled, and able to retrieve remote data. Verify the Brightcove API is currently available., (*238)

The Versions

15/01 2013

dev-master

9999999-dev https://github.com/PlanetTelexInc/PHP-MAPI-Wrapper

Brightcove Media API for PHP

  Sources   Download

Brightcove

The Requires

  • php >=5.2.0
  • ext-curl *

 

video brightcove