【问题标题】:How to upload video to Brightcove through API with PHP and Curl如何使用 PHP 和 Curl 通过 API 将视频上传到 Brightcove
【发布时间】:2016-02-02 05:09:36
【问题描述】:

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

【问题讨论】:

    标签: php curl brightcove


    【解决方案1】:

    最后排序,json 必须是一个参数,然后 file 必须是另一个,像这样:

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

    【讨论】:

      【解决方案2】:

      行:

      '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)
      

      【讨论】:

      • 感谢您的回答,但我仍然遇到同样的错误
      • 我找不到他们的 API 文档,所以不知道如何继续。 JSON 编码的数据字符串实际上会使上传字段不起作用(我只是在发布答案后才注意到)。他们的 API 是否希望您提供他们随后下载的视频的 URL?
      • 它说对于写请求,提交给媒体写API的数据必须是JSON格式,编码为一个名为“json”的表单参数。 JSON 文档应包含一个“方法”字段和一个“参数”字段
      • 我明白了,您的代码是正确的。 file 输入必须是它自己的 JSON 之外的表单元素。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 2018-05-13
      相关资源
      最近更新 更多