我只想使用JSON file_get_contents()方法创建一个来自给定YouTube视频ID的评论列表。
function showYouTubeVideoComments() {
$videoUrl = file_get_contents("https://www.googleapis.com/youtube/v3/commentThreads?part=snippet%2Creplies&videoId=".getYouTubeVideoID()."&key={API_KEY}");
$jsonStuff = json_decode($videoUrl, true);
foreach($jsonStuff['items'] as $val) {
$author = $val['snippet']['authorDisplayName'];
$comment = $val['snippet']['textDisplay'];
$thumbnail_url = $val['snippet']['thumbnails']['default']['url'];
echo '<p><img width="250" src="'.$thumbnail_url.'" align="left"></a>
<a href=”#”>'.$author.'<BR>'.$comment.'</p><br clear=”all”><HR>';
}
}
可以看到,我创建了一个foreach循环来获取所有注释,并用给定的格式回显它们。问题是这没有返回任何信息(没有显示任何内容)。我很确定我没有解析解码的JSON数据。我想我只是不知道该怎么做。
下面是我得到的输出时,使用谷歌YouTube API v3 (YouTube . commentthreads .list) (https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.commentThreads.list?part=snippet%252Creplies&videoId=r-joIGFGzSs&_h=4&):
{
"kind": "youtube#commentThreadListResponse",
"etag": "'"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/OJSTjbDZR4Bu7g7HHvbAGlFePdY'"",
"nextPageToken": "",
"pageInfo": {
"resultsPerPage": 20
},
"items": [
{
"kind": "youtube#commentThread",
"etag": "'"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/_ZIpouGDFNtV6bo_2n_7xGWDjo8'"",
"id": "z124ulhowv2xfrjje04cddoorqzvhzsqphg",
"snippet": {
"channelId": "UCsFmkkSVNgvihycqqtjSXgA",
"videoId": "r-joIGFGzSs",
"topLevelComment": {
"kind": "youtube#comment",
"etag": "'"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/9l4rUkaZa9JV7ed4VD8VgAqq9NA'"",
"id": "z124ulhowv2xfrjje04cddoorqzvhzsqphg",
"snippet": {
"channelId": "UCsFmkkSVNgvihycqqtjSXgA",
"videoId": "r-joIGFGzSs",
"textDisplay": "wowwwwwwwwwwwwwwwwwwww :O'ufeff",
"authorDisplayName": "Blaze Barrios Oficial",
"authorProfileImageUrl": "https://lh3.googleusercontent.com/-5F_WCDGx_FU/AAAAAAAAAAI/AAAAAAAAAH0/qSyREEoI4L8/photo.jpg?sz=50",
"authorChannelUrl": "http://www.youtube.com/channel/UCg539koEkp7LUTbWCVNckAw",
"authorChannelId": {
"value": "UCg539koEkp7LUTbWCVNckAw"
},
"authorGoogleplusProfileUrl": "https://plus.google.com/104808621330256044613",
"canRate": false,
"viewerRating": "none",
"likeCount": 0,
"publishedAt": "2015-08-03T19:19:25.170Z",
"updatedAt": "2015-08-03T19:19:25.170Z"
}
},
"canReply": false,
"totalReplyCount": 0,
"isPublic": true
}
},
{
"kind": "youtube#commentThread",
"etag": "'"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/MuAXxY80_senuqmYNvbL_PT0jtg'"",
"id": "z13zdtir2qucwj3t423vhrepflryfvy1v",
"snippet": {
"channelId": "UCsFmkkSVNgvihycqqtjSXgA",
"videoId": "r-joIGFGzSs",
"topLevelComment": {
"kind": "youtube#comment",
"etag": "'"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/Hypib_8lW3zD4m-QQn-WVpOPnwk'"",
"id": "z13zdtir2qucwj3t423vhrepflryfvy1v",
"snippet": {
"channelId": "UCsFmkkSVNgvihycqqtjSXgA",
"videoId": "r-joIGFGzSs",
"textDisplay": "<b>goosebumps</b>'ufeff",
"authorDisplayName": "MrMcDixi",
"authorProfileImageUrl": "https://lh3.googleusercontent.com/-LKlTLHDGqi0/AAAAAAAAAAI/AAAAAAAAABc/QQll7JSsDeA/photo.jpg?sz=50",
"authorChannelUrl": "http://www.youtube.com/user/MrMcDixi",
"authorChannelId": {
"value": "UCMPWuk7t3dJ7h-2FDO3zIuA"
},
"authorGoogleplusProfileUrl": "https://plus.google.com/116649132474022071199",
"canRate": false,
"viewerRating": "none",
"likeCount": 0,
"publishedAt": "2015-08-03T15:42:52.425Z",
"updatedAt": "2015-08-03T15:42:52.425Z"
}
},
"canReply": false,
"totalReplyCount": 0,
"isPublic": true
}
},
{
"kind": "youtube#commentThread",
"etag": "'"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/6rymqoB3i9-840FIjrTUoAZXnR8'"",
"id": "z131f3k5jyy4xd0fx23zizhgymncen45a04",
"snippet": {
"channelId": "UCsFmkkSVNgvihycqqtjSXgA",
"videoId": "r-joIGFGzSs",
"topLevelComment": {
"kind": "youtube#comment",
"etag": "'"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/IbsIF7pVccsXO9R50SlzUvupmg4'"",
"id": "z131f3k5jyy4xd0fx23zizhgymncen45a04",
"snippet": {
"channelId": "UCsFmkkSVNgvihycqqtjSXgA",
"videoId": "r-joIGFGzSs",
"textDisplay": "This real 100/10 this beats tells a story or a lesson. Depending on the artists<br />'ufeff",
"authorDisplayName": "Dakidd369",
"authorProfileImageUrl": "https://lh6.googleusercontent.com/-UG45lvZDbRQ/AAAAAAAAAAI/AAAAAAAAAEs/TaX45400Ubw/photo.jpg?sz=50",
"authorChannelUrl": "http://www.youtube.com/user/Dakidd369",
"authorChannelId": {
"value": "UCiX0bpC8jFvs77LNcCtQSlw"
},
"authorGoogleplusProfileUrl": "https://plus.google.com/108165851390080260713",
"canRate": false,
"viewerRating": "none",
"likeCount": 1,
"publishedAt": "2015-08-03T07:03:25.968Z",
"updatedAt": "2015-08-03T07:03:25.968Z"
}
},
"canReply": false,
"totalReplyCount": 0,
"isPublic": true
}
}
]
}
请帮我弄清楚如何解析这些数据。谢谢你。
我明白了。我会张贴我的功能,以防其他人在那里需要类似的帮助。我已经添加了注释来帮助解释发生了什么。
该函数使用Google的YouTube API v3,简单地显示指定视频ID的顶级评论。此函数不显示评论回复。
格式将显示作者名称,作者缩略图和注释。我使用的任何div或span类都是专门针对我的网站的,但当然可以根据您的喜好自定义。
/* Function to get YouTube video comments */
function showYouTubeVideoComments() {
$videoUrl = file_get_contents("https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&videoId={ADD_VIDEO_ID_HERE}&key={ADD_API_KEY_HERE}"); //Get contents of Video URL with specified video ID.
$jsonStuff = json_decode($videoUrl, true); //Decode API Response.
if (empty($jsonStuff)) { echo "No comments were found."; } //If there are no comments, tell the user.
/* Display each comment 'item' in array */
foreach($jsonStuff['items'] as $val) {
$author = $val['snippet']['topLevelComment']['snippet']['authorDisplayName']; //Get Comment Author Name.
$author_url = $val['snippet']['topLevelComment']['snippet']['authorChannelUrl']; //Get Comment Author URL.
$author_thumbnail_url = $val['snippet']['topLevelComment']['snippet']['authorProfileImageUrl']; //Get Comment Author Thumbnail URL.
$comment = $val['snippet']['topLevelComment']['snippet']['textDisplay']; //Get Comment Content.
/* Echo information in the following layout */
echo '<div class="hbox">
<div class="col-lg-1"><span class="pull-left thumb-sm avatar m-r"><a href="'.$author_url.'" target="_blank"><img width="250" src="'.$author_thumbnail_url.'"></a></span></div>
<div class="col-lg-2"><a href="'.$author_url.'" target="_blank">'.$author.'</a></div>
<div class="col-lg-9"><small class="youtube-comment-container text-muted clear text-ellipsis">'.$comment.'</small></div>
<br clear=”all”>
</div><hr>';
}
}
要在您的主题中使用它,只需将此代码放置在需要显示注释的地方。
<?php showYouTubeVideoComments(); ?>