[英]CURL fail to send POST request with filestream
嘗試將文件流和其他數據發送到API,但返回錯誤:
The video provided was null
這是我的代碼:
function create_video($files) {
$api = "http://api.brightcove.com/services/post";
$local_file_list = $files['file']['tmp_name'];
foreach ($local_file_list as $local_file) {
try {
$fp = fopen($local_file, 'r');
$ch = curl_init();
if (FALSE === $ch) {
throw new Exception('failed to initialize');
}
//"video":{"name":"test","shortDescription":"test","startDate":1432282741000,"endDate":null}
$request = 'json={"params":{"encode_to":"MP4","create_multiple_renditions":"True","token":"' . WRITE_TOKEN . '"},"method":"create_video"}';
curl_setopt($ch, CURLOPT_URL, $api);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($local_file));
$content = curl_exec($ch);
if (FALSE === $content) {
throw new Exception(curl_error($ch), curl_errno($ch));
}
die(var_dump(json_decode($content)));
return json_decode($content);
} catch (Exception $e) {
trigger_error(sprintf('Curl failed with error #%d: %s', $e->getCode(), $e->getMessage()), E_USER_ERROR);
}
}
}
幾件事需要注意:
- CURL不返回錯誤,因此可能不是CURL引起的
filesize($local_file)
已返回正確的文件大小- fopen($ local_file,'r'); 返回流類型的內容,因此該文件存在
如何解決? 謝謝
API參考:
https://docs.brightcove.com/en/video-cloud/media/references/reference.html#Video_Write
更新:
感謝您的幫助,但現在返回了另一個錯誤:
POST methods require valid JSON-RPC in the POST body, with "method" and "params" properties
我有一些想法,當我查看請求有效負載時,它與正確的負載略有不同:
當前我的版本:
------WebKitFormBoundary8VABz8KuNRE8Hepd
Content-Disposition: form-data; name="file[0]"; filename="big_buck_bunny.mp4"
Content-Type: video/mp4
------WebKitFormBoundary8VABz8KuNRE8Hepd--
正確的版本
------WebKitFormBoundaryCAB6WEANBJxoB3Op
Content-Disposition: form-data; name="JSONRPC"
{"params":{"video":{"name":"test","shortDescription":"test","startDate":1432282741000,"endDate":null},"encode_to":"MP4","create_multiple_renditions":"True","token":"VyocgALDnxU8HPvmnSnckgmXjoPlYWomc2La5Tn-evuAfsnSPJJoow.."},"method":"create_video"}
------WebKitFormBoundaryCAB6WEANBJxoB3Op
Content-Disposition: form-data; name="filePath"; filename="big_buck_bunny.mp4"
Content-Type: video/mp4
------WebKitFormBoundaryCAB6WEANBJxoB3Op
Content-Disposition: form-data; name="JSONView"
{"params":{"video":{"name":"test","shortDescription":"test","startDate":1432282741000,"endDate":null},"encode_to":"MP4","create_multiple_renditions":"True","token":"VyocgALDnxU8HPvmnSnckgmXjoPlYWomc2La5Tn-evuAfsnSPJJoow.."},"method":"create_video"}
------WebKitFormBoundaryCAB6WEANBJxoB3Op--
您可以包裝文件以上傳到CURLFile
(PHP> = 5.5):
$cfile = new CURLFile($local_file, 'video/mp4', basename($local_file));
然后,添加到CURLOPT_POSTFIELDS
,使其成為整個請求的一部分:
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'file' => $cfile,
'JSONRPC' => '{"params":{"encode_to":"MP4","create_multiple_renditions":"True","token":"' . WRITE_TOKEN . '"},"method":"create_video"}';
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
在PHP 5.5之前,您可以使用@
表示文件:
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'file' => "@$local_file",
'JSONRPC' => '{"params":{"encode_to":"MP4","create_multiple_renditions":"True","token":"' . WRITE_TOKEN . '"},"method":"create_video"}';
]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.