gpt4 book ai didi

php - 使用 curl 和 api v3 在 Youtube 上上传视频

转载 作者:可可西里 更新时间:2023-10-31 22:09:12 25 4
gpt4 key购买 nike

我会在 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;
}

但是它返回这个

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
)

)

文件是 MP4。

有人可以帮忙吗?

最佳答案

更新版本:现在具有自定义上传 URL 并在上传过程中发送元数据。整个过程需要2个请求:

  1. 获取自定义上传位置

    首先,发出上传 URL 的 POST 请求:

    "https://www.googleapis.com/upload/youtube/v3/videos"

    您需要发送 2 个 header :

    "Authorization": "Bearer {YOUR_ACCESS_TOKEN}"
    "Content-type": "application/json"

    你需要发送3个参数:

    "uploadType": "resumable"
    "part": "snippet, status"
    "key": {YOUR_API_KEY}

    并且您需要在请求正文中发送视频的元数据:

        {
    "snippet": {
    "title": {VIDEO TITLE},
    "description": {VIDEO DESCRIPTION},
    "tags": [{TAGS LIST}],
    "categoryId": {YOUTUBE CATEGORY ID}
    },
    "status": {
    "privacyStatus": {"public", "unlisted" OR "private"}
    }
    }

    从这个请求中,您应该得到一个在 header 中带有“位置”字段的响应。

  2. POST 到自定义位置以发送文件。

    对于上传,您需要 1 个标题:

    "Authorization": "Bearer {YOUR_ACCESS_TOKEN}"

    并将文件作为您的数据/正文发送。

如果您通读了他们的客户端的工作原理,您会发现如果您返回代码 500、502、503 或 504 的错误,他们会建议您重试。很明显,您希望在重试和最大重试次数之间有一个等待时间.它每次都在我的系统中运行,尽管我使用的是 python 和 urllib2 而不是 cURL。

此外,由于自定义上传位置,此版本具有上传可恢复功能,尽管我还需要它。

关于php - 使用 curl 和 api v3 在 Youtube 上上传视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13274173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com