在 PHP 中使用 Youtube API V3 检索缩略图和视频时间
Retrieving Thumbnail and Video Time Using Youtube API V3 in PHP
我跟着这个啧啧 https://packagist.org/packages/alaouy/youtube
一切顺利,但我在这段代码中遇到了问题:
$video = Youtube::getVideoInfo('rie-hPVJ7Sw');
var_dump($video);
这是我得到的回复:
object(stdClass)#712 (8) { ["kind"]=> string(13) "youtube#video"
["etag"]=> string(57)
""iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/XCpzrqbmTpMEPyMBSGQ0oz6NLe4"" ["id"]=>
string(11) "rie-hPVJ7Sw" ["snippet"]=> object(stdClass)#713 (10) {
["publishedAt"]=> string(24) "2013-03-21T02:28:12.000Z"
["channelId"]=> string(24) "UC5ENZAI7prEaHGW1hPgOQEQ" ["title"]=>
string(48) "Sergey Brin talks about Google Glass at TED 2013"
["description"]=> string(479) "UPDATE: To address comments about
Sergey's poor delivery, I want to emphasize that this is NOT a "TED
Talk", despite it being recorded during TED conference. It is pretty
much a spontaneous appearance to show the latest technology and wasn't
prepared or rehearsed. Google Glass is also not available for purchase
yet so it is not strictly speaking a product promotion either. This
video is posted mostly because it has details about Glass that were
unknown or unconfirmed before." ["thumbnails"]=> object(stdClass)#714
(3) { ["default"]=> object(stdClass)#715 (3) { ["url"]=> string(46)
"https://i.ytimg.com/vi/rie-hPVJ7Sw/default.jpg" ["width"]=> int(120)
["height"]=> int(90) } ["medium"]=> object(stdClass)#716 (3) {
["url"]=> string(48)
"https://i.ytimg.com/vi/rie-hPVJ7Sw/mqdefault.jpg" ["width"]=>
int(320) ["height"]=> int(180) } ["high"]=> object(stdClass)#717 (3) {
["url"]=> string(48)
"https://i.ytimg.com/vi/rie-hPVJ7Sw/hqdefault.jpg" ["width"]=>
int(480) ["height"]=> int(360) } } ["channelTitle"]=> string(8)
"tedleaks" ["tags"]=> array(4) { [0]=> string(11) "sergey brin" [1]=>
string(6) "google" [2]=> string(12) "google glass" [3]=> string(3)
"ted" } ["categoryId"]=> string(2) "28" ["liveBroadcastContent"]=>
string(4) "none" ["localized"]=> object(stdClass)#718 (2) {
["title"]=> string(48) "Sergey Brin talks about Google Glass at TED
2013" ["description"]=> string(479) "UPDATE: To address comments about
Sergey's poor delivery, I want to emphasize that this is NOT a "TED
Talk", despite it being recorded during TED conference. It is pretty
much a spontaneous appearance to show the latest technology and wasn't
prepared or rehearsed. Google Glass is also not available for purchase
yet so it is not strictly speaking a product promotion either. This
video is posted mostly because it has details about Glass that were
unknown or unconfirmed before." } } ["contentDetails"]=>
object(stdClass)#719 (5) { ["duration"]=> string(8) "PT13M30S"
["dimension"]=> string(2) "2d" ["definition"]=> string(2) "sd"
["caption"]=> string(5) "false" ["licensedContent"]=> bool(false) }
["status"]=> object(stdClass)#720 (5) { ["uploadStatus"]=> string(9)
"processed" ["privacyStatus"]=> string(6) "public" ["license"]=>
string(7) "youtube" ["embeddable"]=> bool(true)
["publicStatsViewable"]=> bool(true) } ["statistics"]=>
object(stdClass)#721 (5) { ["viewCount"]=> string(6) "539812"
["likeCount"]=> string(4) "2450" ["dislikeCount"]=> string(3) "371"
["favoriteCount"]=> string(1) "0" ["commentCount"]=> string(4) "2247"
} ["player"]=> object(stdClass)#722 (1) { ["embedHtml"]=> string(116)
"
抱歉输出行太长了。
我要的是视频时长、缩略图和视频标题。
我想它应该是这样的形式。
<?php
foreach($item as $video)
{
echo $item['snippet']['thumbnails]['default'];
}
?>
类似的东西不断抛出错误。
这是一个标准类,不是数组。获取信息的正确方法是
<?php
$video = Youtube::getVideoInfo('rie-hPVJ7Sw');
echo $video->snippet->thumbnails->default->url;
?>
我跟着这个啧啧 https://packagist.org/packages/alaouy/youtube
一切顺利,但我在这段代码中遇到了问题:
$video = Youtube::getVideoInfo('rie-hPVJ7Sw');
var_dump($video);
这是我得到的回复:
object(stdClass)#712 (8) { ["kind"]=> string(13) "youtube#video" ["etag"]=> string(57) ""iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/XCpzrqbmTpMEPyMBSGQ0oz6NLe4"" ["id"]=> string(11) "rie-hPVJ7Sw" ["snippet"]=> object(stdClass)#713 (10) { ["publishedAt"]=> string(24) "2013-03-21T02:28:12.000Z" ["channelId"]=> string(24) "UC5ENZAI7prEaHGW1hPgOQEQ" ["title"]=> string(48) "Sergey Brin talks about Google Glass at TED 2013" ["description"]=> string(479) "UPDATE: To address comments about Sergey's poor delivery, I want to emphasize that this is NOT a "TED Talk", despite it being recorded during TED conference. It is pretty much a spontaneous appearance to show the latest technology and wasn't prepared or rehearsed. Google Glass is also not available for purchase yet so it is not strictly speaking a product promotion either. This video is posted mostly because it has details about Glass that were unknown or unconfirmed before." ["thumbnails"]=> object(stdClass)#714 (3) { ["default"]=> object(stdClass)#715 (3) { ["url"]=> string(46) "https://i.ytimg.com/vi/rie-hPVJ7Sw/default.jpg" ["width"]=> int(120) ["height"]=> int(90) } ["medium"]=> object(stdClass)#716 (3) { ["url"]=> string(48) "https://i.ytimg.com/vi/rie-hPVJ7Sw/mqdefault.jpg" ["width"]=> int(320) ["height"]=> int(180) } ["high"]=> object(stdClass)#717 (3) { ["url"]=> string(48) "https://i.ytimg.com/vi/rie-hPVJ7Sw/hqdefault.jpg" ["width"]=> int(480) ["height"]=> int(360) } } ["channelTitle"]=> string(8) "tedleaks" ["tags"]=> array(4) { [0]=> string(11) "sergey brin" [1]=> string(6) "google" [2]=> string(12) "google glass" [3]=> string(3) "ted" } ["categoryId"]=> string(2) "28" ["liveBroadcastContent"]=> string(4) "none" ["localized"]=> object(stdClass)#718 (2) { ["title"]=> string(48) "Sergey Brin talks about Google Glass at TED 2013" ["description"]=> string(479) "UPDATE: To address comments about Sergey's poor delivery, I want to emphasize that this is NOT a "TED Talk", despite it being recorded during TED conference. It is pretty much a spontaneous appearance to show the latest technology and wasn't prepared or rehearsed. Google Glass is also not available for purchase yet so it is not strictly speaking a product promotion either. This video is posted mostly because it has details about Glass that were unknown or unconfirmed before." } } ["contentDetails"]=> object(stdClass)#719 (5) { ["duration"]=> string(8) "PT13M30S" ["dimension"]=> string(2) "2d" ["definition"]=> string(2) "sd" ["caption"]=> string(5) "false" ["licensedContent"]=> bool(false) } ["status"]=> object(stdClass)#720 (5) { ["uploadStatus"]=> string(9) "processed" ["privacyStatus"]=> string(6) "public" ["license"]=> string(7) "youtube" ["embeddable"]=> bool(true) ["publicStatsViewable"]=> bool(true) } ["statistics"]=> object(stdClass)#721 (5) { ["viewCount"]=> string(6) "539812" ["likeCount"]=> string(4) "2450" ["dislikeCount"]=> string(3) "371" ["favoriteCount"]=> string(1) "0" ["commentCount"]=> string(4) "2247" } ["player"]=> object(stdClass)#722 (1) { ["embedHtml"]=> string(116) "
抱歉输出行太长了。
我要的是视频时长、缩略图和视频标题。
我想它应该是这样的形式。
<?php
foreach($item as $video)
{
echo $item['snippet']['thumbnails]['default'];
}
?>
类似的东西不断抛出错误。
这是一个标准类,不是数组。获取信息的正确方法是
<?php
$video = Youtube::getVideoInfo('rie-hPVJ7Sw');
echo $video->snippet->thumbnails->default->url;
?>