2016-02-01 60 views
1

我試圖從他們的API使用create_video方法將視頻上傳到Brightcove。他們的所有示例都顯示瞭如何從已從表單提交的圖像中執行此操作。我需要用已經在服務器上的圖像來完成它。我覺得我很接近,但我不斷收到以下錯誤:如何通過PHP和Curl通過API將視頻上傳到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); 

回答

1

到底排序它,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)); 
0

行:

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

結果張貼file場其價值是視頻文件名稱的完整路徑。

爲了獲得捲曲發佈它作爲一個實際的文件上傳,用@像這樣前綴的路徑:

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

如果你使用PHP 5.5或更高,你應該使用CURLFile對象上傳字段:

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

感謝您的回答我仍然得到相同的錯誤,雖然 – geoffs3310

+0

我找不到他們的API文檔,所以不知道如何繼續。 JSON編碼的數據字符串實際上會使上傳字段不起作用(我只是注意到,我發佈我的答案後)。他們的API是否希望你提供一個URL到他們下載的視頻? – drew010

+0

這是API文檔的URL http://docs.brightcove.com/en/video-cloud/media/references/reference.html#Video_Write – geoffs3310

相關問題