使用PHP的curl功能使用其v3 API将本地mp4视频(文件大小为20 MB)上传到YouTube,这让我发疯。我有1个YouTube帐户,每个视频都应从我的网站(目前正在localhost(xampp)运行)中上传到该帐户。
我知道那里有一些不错的第三方库,但是我不想使用它们。我只想使用 PHP的curl 函数上传视频。
我知道我必须生成一个客户端ID ,客户端机密和一个 oauth授权代码,所以我在{{3 }}。
当我尝试上传本地mp4视频时,PHP本身没有任何错误。因此,登录YouTube后访问my videos
时,我看到有1个新添加的视频,但是它无法处理。 YouTube会自动将标题和描述设置为“未知”,这似乎与Google's Playground中的问题相同。
也许我的PHP代码有问题,还是我错过了一些事情?
$info = new stdClass();
$info->snippet = array();
$info->snippet['title'] = 'Test video';
$info->snippet['description'] = 'This is a upload from PHP';
$info->snippet['categoryId'] = 27;
$info->snippet['tags'] = array('test', 'video');
$info->snippet['privacyStatus'] = 'public';
$res = json_encode($info);
$params = array(
'part' => 'snippet',
'file' => 'uploads/files/video.mp4',
'video' => $res
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/upload/youtube/v3/videos?part=snippet');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/octet-stream','Authorization: Bearer '.$token->access_token));
$return = json_decode(curl_exec($ch));
curl_close($ch);
var_dump($return);
结果
stdClass Object
(
[kind] => youtube#video
[etag] => "XXXXXX-eQIiCIp7p4ldoVcVXXXY/7PWngKZaXXXXMIvnZu6XX4GXXXX"
[id] => XXXXXXBXXXX
[snippet] => stdClass Object
(
[publishedAt] => 2018-06-30T16:02:01.000Z
[channelId] => XXXZASuXXXZc5KXXXa4wTXXX
[title] => unknown
[description] =>
[thumbnails] => stdClass Object
(
[default] => stdClass Object
(
[url] => https://i.ytimg.com/vi/ZAVad2BaDSU/default.jpg
[width] => 120
[height] => 90
)
[medium] => stdClass Object
(
[url] => https://i.ytimg.com/vi/ZAVad2BaDSU/mqdefault.jpg
[width] => 320
[height] => 180
)
[high] => stdClass Object
(
[url] => https://i.ytimg.com/vi/ZAVad2BaDSU/hqdefault.jpg
[width] => 480
[height] => 360
)
)
[channelTitle] => example
[categoryId] => 22
[liveBroadcastContent] => none
[localized] => stdClass Object
(
[title] => unknown
[description] =>
)
)
)