ItsMeMike ItsMeMike - 4 months ago 38
JSON Question

Parsing data from YouTube Comments API V3 with JSON

I have tried and tried to figure this out. Been at this for days and it is driving me crazy. I've searched and experimented and tried many snippets. This should be a straight forward task, but I can't figure it out.

I simply want to create a list of comments from a given YouTube video ID, using the JSON file_get_contents() method.

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>';
}

}


As you can see, I'm creating a foreach loop to get all comments and echo them with the given format. The problem is that this returns no information (nothing is displayed). I'm pretty sure I'm not parsing the decoded JSON data. I just don't know how to do it, I guess.

Below is the output I get when using the 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
}
}
]
}


Can anyone please, please help me figure out how to parse this data? I will be forever grateful. Thank you.

M!ke

Answer

I figured it out. I'll post the function I made incase others out there need similar help. I've added comments to help explain what's going on.

This function simply displays the top-level comments from specified video ID, using Google's YouTube API v3. This function does not display comment replies.

The formatting will display Author Name, Author Thumbnail, and Comment. Any div or span classes I used are specifically for my site, but of course customize to your liking.

/* 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>';
    }

}

To use this in your theme, simply place this code wherever you need to display the comments.

<?php showYouTubeVideoComments(); ?>

Hope this helps.