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

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

我正在尝试使用 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);

行:

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

导致 POSTing file 字段,其值是视频文件名称的完整路径。

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

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

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

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

最后排序了,json 必须是一个参数,然后文件必须是另一个参数,如下所示:

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