Create media

Headers are set up according to Authentification pre-requisite specifications.

In this example, the creation of the full media entity is performed in 2 steps:
- Creation of thumbnails
- Creation of the Media

Creation of thumbnails

We admit the $media_thumbnails variable contains an array of pictures urls representing the media entity.

The MediaElement creation request needs at least a location (url of the thumbnail image).

On creation success, we store the created MediaElement's id in an array called $thumbnailsMediaElementsIDs which will be used for the Media creation request.

Creation of the Media

The Media creation request takes a name, a mime type (ex.: video/x-custom if this media is not generic and can be used only with you player), optionnally a xkey (if custom key is needed to instanciate the media in your player) and your thumbnails array just created.

If you want to create a noncustom Media entity such as video/mp4, which can be read throught multiple web players, you need to create MediaElements like for the thumbnails but with the video/mp4 streams and add it the request in the "assets" field.

On success, you will get a Media entity.

    //$media_thumbnails: array of thumbnails jpeg for example

    $thumbnailsMediaElementsIDs = array();
    $createdMediaEntityID = null;

    // Create all media elements which will be used as thumbnails for the media
    foreach($media_thumbnails as $thumbnail_url) {
        // Get cURL resource
        $curl = curl_init();

        // Server EndPoint
        $url = 'https://services.adways.com/media-element';

        $method = 'POST';

        $fields = array();
        $fields['location'] = $thumbnail_url;

        $options = array();
        $options[CURLOPT_RETURNTRANSFER] = 1;
        $options[CURLOPT_URL] = $url;
        $options[CURLOPT_SSL_VERIFYPEER] = false;
        $options[CURLOPT_POST] = 1;

        if(!empty($fields)) $options[CURLOPT_POSTFIELDS] = json_encode($fields);

        curl_setopt_array($curl, $options);

        // $access_token: your access token, from your platform
        // X-Token-Provider: to identify the source of the given token
        $headers = array();
        $headers[] = 'Authorization: Bearer ' . $access_token;
        $headers[] = 'X-Token-Provider: ' . $tokenProviderConstant;

        curl_setopt($curl,CURLOPT_HTTPHEADER, $headers);


        // Send the request & save response to $response
        $response = json_decode(curl_exec($curl), true);

        // Save Curl Info request
        $curlInfos = curl_getinfo($curl);

        // Close request to clear up some resources
        curl_close($curl);

        if($curlInfos['http_code'] == 201) {
                $thumbnailsMediaElementsIDs[] = $response['id'];
        }
    }

    // Get cURL resource
    $curl = curl_init();

    // Server EndPoint
    $url = 'https://services.adways.com/media';

    $method = 'POST';

    $fields = array();
    $fields['name'] = $media_name;
    $fields['xkey'] = $media_xkey;
    $fields['mime'] = $media_mime;
    $fields['thumbnails'] = $thumbnailsMediaElementsIDs;

    $options = array();
    $options[CURLOPT_RETURNTRANSFER] = 1;
    $options[CURLOPT_URL] = $url;
    $options[CURLOPT_SSL_VERIFYPEER] = false;
    $options[CURLOPT_POST] = 1;

    if(!empty($fields)) $options[CURLOPT_POSTFIELDS] = json_encode($fields);

    curl_setopt_array($curl, $options);

    $headers = array();
    $headers[] = 'Authorization: Bearer ' . $access_token;
    $headers[] = 'X-Token-Provider: ' . $tokenProviderConstant;

    curl_setopt($curl,CURLOPT_HTTPHEADER, $headers);

    // Send the request & save response to $response
    $response = json_decode(curl_exec($curl), true);

    // Save Curl Info request
    $curlInfos = curl_getinfo($curl);

    // Close request to clear up some resources
    curl_close($curl);

    if($curlInfos['http_code'] == 201) {
            $createdMediaEntityID = $response['id'];
    }

For more informations: API Media & API MediaElement

In the example we store the Media entity id in a variable called: $createdMediaEntityID. This Media Entity id can be used to create or update an HVProject in the "media" field.