使用 JSON 解析来自 YouTube Comments API V3 的数据

Parsing data from YouTube Comments API V3 with JSON

我只想使用 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 循环以获取所有评论并使用给定格式回显它们。问题是这个 returns 没有信息(什么都不显示)。我很确定我没有解析解码的 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
   }
  }
 ]
}

请帮我弄清楚如何解析这些数据。谢谢。

我明白了。我将 post 我制作的功能,以防其他人需要类似的帮助。我添加了评论以帮助解释发生了什么。

此函数仅显示来自指定视频 ID 的顶级评论,使用 Google 的 YouTube API v3。此功能不显示评论回复。

格式将显示作者姓名、作者缩略图和评论。我使用的任何 div 或跨度 类 都是专门针对我的网站的,但当然可以根据您的喜好进行定制。

/* 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(); ?>

希望对您有所帮助。