gen*_*nna 11 php upload curl youtube-api
我会在PHP中使用带有curl的Youtube API v3上传视频,如下所述:https://developers.google.com/youtube/v3/docs/videos/insert
我有这个功能
function uploadVideo($file, $title, $description, $tags, $categoryId, $privacy)
{
$token = getToken(); // Tested function to retrieve the correct AuthToken
$video->snippet['title'] = $title;
$video->snippet['description'] = $description;
$video->snippet['categoryId'] = $categoryId;
$video->snippet['tags'] = $tags; // array
$video->snippet['privacyStatus'] = $privacy;
$res = json_encode($video);
$parms = array(
'part' => 'snippet',
'file' => '@'.$_SERVER['DOCUMENT_ROOT'].'/complete/path/to/'.$file
'video' => $res
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/upload/youtube/v3/videos');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parms);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$token['access_token']));
$return = json_decode(curl_exec($ch));
curl_close($ch);
return $return;
}
Run Code Online (Sandbox Code Playgroud)
但它返回了这一点
stdClass Object
(
[error] => stdClass Object
(
[errors] => Array
(
[0] => stdClass Object
(
[domain] => global
[reason] => badContent
[message] => Unsupported content with type: application/octet-stream
)
)
[code] => 400
[message] => Unsupported content with type: application/octet-stream
)
)
Run Code Online (Sandbox Code Playgroud)
该文件是MP4.
有人可以帮忙吗?
Cha*_*fus 21
更新版本:现在使用自定义上传网址并通过上传过程发送元数据.整个过程需要2个请求:
获取自定义上传位置
首先,对上传网址发出POST请求:
"https://www.googleapis.com/upload/youtube/v3/videos"
Run Code Online (Sandbox Code Playgroud)
您需要发送2个标头:
"Authorization": "Bearer {YOUR_ACCESS_TOKEN}"
"Content-type": "application/json"
Run Code Online (Sandbox Code Playgroud)
您需要发送3个参数:
"uploadType": "resumable"
"part": "snippet, status"
"key": {YOUR_API_KEY}
Run Code Online (Sandbox Code Playgroud)
您需要在请求正文中发送视频的元数据:
{
"snippet": {
"title": {VIDEO TITLE},
"description": {VIDEO DESCRIPTION},
"tags": [{TAGS LIST}],
"categoryId": {YOUTUBE CATEGORY ID}
},
"status": {
"privacyStatus": {"public", "unlisted" OR "private"}
}
}
Run Code Online (Sandbox Code Playgroud)
根据此请求,您应该在标头中获得带有"位置"字段的回复.
POST到自定义位置发送文件.
对于上传,您需要1个标题:
"Authorization": "Bearer {YOUR_ACCESS_TOKEN}"
Run Code Online (Sandbox Code Playgroud)
并将文件作为您的数据/正文发送.
如果您了解其客户端的工作原理,您会看到他们建议您在返回错误代码500,502,503或504时重试.显然,您需要在重试和最大重试次数之间有一段等待时间.它每次都在我的系统中工作,虽然我使用的是python和urllib2而不是cURL.
此外,由于自定义上传位置,此版本具有上载可恢复功能,但我尚未需要.
不幸的是,我们还没有从 PHP 上传 YouTube API v3 的具体示例,但我的一般建议是:
一般来说,您的 cURL 代码有很多不正确的地方,我无法完成修复它所需的所有步骤,因为我认为使用 PHP 客户端库是一个更好的选择。如果您确信要使用 cURL,那么我将听取其他人提供的具体指导。