使用curl和api v3在YouTube上上传视频 [英] Upload video on Youtube using curl and api v3
问题描述
我会使用PHP API中的curl以Youtube API v3上传视频,如下所示: 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。
- 将您的代码置于此示例(针对云端硬盘写入)上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.
这篇关于使用curl和api v3在YouTube上上传视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!