如何使用PHP和Curl通过API将视频上传到Brightcove

时间:2016-02-01 19:40:12

标签: php curl brightcove

我正在尝试使用其API中的create_video方法将视频上传到Brightcove。他们的所有示例都显示了如何从已从表单提交的图像中执行此操作。我需要使用服务器上已有的图像。我想我很接近,但我一直收到以下错误:

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

这是我的代码:

$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);

2 个答案:

答案 0 :(得分:1)

最后将它排序,json必须是一个参数,然后文件必须是另一个像这样:

Rows

答案 1 :(得分:0)

该行:

'file' => FCPATH.'assets/uploads/submitted/'.$video->filename

会导致file字段张贴,其值为视频文件名称的完整路径。

要让cURL将其作为实际文件上传发布,请在路径前加上@,如下所示:

'file' => '@' . FCPATH.'assets/uploads/submitted/'.$video->filename

如果您使用的是PHP 5.5或更高版本,则应使用CURLFile对象作为上传字段:

'file' => new CURLFile(FCPATH.'assets/uploads/submitted/'.$video->filename)