使用 curl 和 api v3 在 Youtube 上上传视频
我将使用 Youtube API v3 和 PHP 中的 curl 上传视频,如下所述:https://developers.google.com/youtube/v3/docs/videos/insert
I would upload a video using the Youtube API v3 with curl in PHP, as described here: 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.
有人可以帮忙吗?
很遗憾,我们还没有从 PHP 上传 YouTube API v3 的具体示例,但我的一般建议是:
Unfortunately, we don't have a specific example of YouTube API v3 uploads from PHP available yet, but my general advice is:
- 使用PHP 客户端库 而不是 cURL.莉>
- 基于为 Drive API 编写的此示例编写代码.由于 YouTube API v3 与其他 Google API 共享通用 API 基础架构,因此在不同服务中执行上传文件等操作的示例应该非常相似.
- 查看 Python 示例,了解具体的需要在 YouTube v3 上传中设置的元数据.
- Use the PHP client library instead of cURL.
- Base your code on this example written for the Drive API. Because the YouTube API v3 shares a common API infrastructure with other Google APIs, examples for doing things like uploading files should be very similar across different services.
- Take a look at the Python example for the specific metadata that needs to be set in a YouTube v3 upload.
总的来说,您的 cURL 代码有很多不正确的地方,我无法完成修复它所需的所有步骤,因为我认为使用 PHP 客户端库是一个更好的选择.如果您确定要使用 cURL,那么我会请其他人提供具体指导.
In general, there are a lot of things incorrect with your cURL code, and I can't walk through all the steps it would take to fix it, as I think using the PHP client library is a much better option. If you are convinced you want to use cURL then I'll defer to someone else to provide specific guidance.