我试过并尝试解决这个问题。已经好几天了,这让我发疯了。我搜索并试验了很多片段。这应该是一项直接的任务,但我无法弄清楚。
我只是想使用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数据。我想,我只是不知道该怎么做。
以下是使用Google 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
}
}
]
}
任何人都可以请,请帮我弄清楚如何解析这些数据?我将永远感激。谢谢。
中号!科
答案 0 :(得分:1)
我明白了。我会发布我做的功能,其他人需要类似的帮助。我已添加评论以帮助解释发生了什么。
此功能只使用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(); ?>
希望这有帮助。