Search code examples
phpcurlbrightcove

How to upload video to Brightcove through API with PHP and Curl


I am trying to upload a video to Brightcove using the create_video method from their API. All of their examples show how to do it from an image that has been submitted from a form. I need to do it with images that are already on the server though. I think I'm quite close but I keep getting the following error:

{"error": {"name":"RequiredParameterError","message":"create_video requires a filestream or remote asset references","code":303}, "result": null, "id": null}1

Here is my code:

$data = array(
    'method' => "create_video",
    'params' => array(
        'video' => array(
            'name' => $video->filename,
            'referenceId' => $video->id,
            'shortDescription' => 'Sample video'
        ),
        "token" => $this->config->item('brightcove_write_token'), 
        "encode_to" => "MP4",
        "create_multiple_renditions" => "True",
    ),
    'filename' => $video->filename,
    'file' => FCPATH.'assets/uploads/submitted/'.$video->filename
);                                                                    
$data_string = json_encode($data);    

$url = 'http://api.brightcove.com/services/post';
$fields = array(
    'json' => $data_string
);

//url-ify the data for the POST
$fields_string='';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

Solution

  • Sorted it in the end, json had to be one parameter and then file had to be another like so:

    $data = array(
        'method' => "create_video",
        'params' => array(
            'video' => array(
                'name' => $video->filename,
                'shortDescription' => 'Sample video'
            ),
            "token" => $this->config->item('brightcove_write_token'), 
            "encode_to" => "MP4",
            "create_multiple_renditions" => "True"
        ),
    );                                                                    
    $data_string = json_encode($data);    
    
    $url = 'http://api.brightcove.com/services/post';
    $fields = array(
        'json' => $data_string,
        'file' => new CURLFile(FCPATH.'assets/uploads/submitted/'.$video->filename)
    );
    
    //open connection
    $ch = curl_init();
    
    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    //execute post
    $result = json_decode(curl_exec($ch));