Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I display the replies in youtube comments.

Here's my code

PHP
$videoid = 'UKdZU9Db6fk';
$apikey = 'API KEY';

$json = file_get_contents('https://www.googleapis.com/youtube/v3/commentThreads?maxResults=100&part=snippet%2Creplies&videoId='.$videoid.'&key='.$apikey);
$ytdata = json_decode($json);

foreach ($ytdata->items as $item) {
    echo "Author: ". $item->snippet->topLevelComment->snippet->authorDisplayName;
    echo "Comment: ". $item->snippet->topLevelComment->snippet->textDisplay;

    foreach ($ytdata->items->replies as $iteminner) {
	    echo "Reply: ". $iteminner->comments[0]->textDisplay;
    }
}


I can display the comment but the reply into the specific comment is the one that I can't do, also what if there are comments that has it's own replies.

Thanks

What I have tried:

I've been searching on the net and most questions are the same they also have the same problems.

I'm trying to add foreach inside foreach but the result is

"Trying to get property of non-object"
"Invalid argument supplied for foreach() "


Here's how the xml data

https://www.googleapis.com/youtube/v3/commentThreads?maxResults=100&part=snippet%2Creplies&videoId=UKdZU9Db6fk&key=AIzaSyCC9AeTosJL21zfVGf8aVPPh9Tz5KTyQGM

Thanks
Posted
Updated 28-Jun-19 0:15am
v3
Comments
Richard MacCutchan 16-Aug-17 3:38am    
"Invalid argument supplied for foreach() "
The message is telling you what is wrong, the object reference you are trying to use is not valid.

The result from the API has 'items' ONLY if it was successful, other wise it has no and your code fails...
You have to check the return value for error code, before processing the data...
A possible error you may have, and the structure of the result in error case:
JavaScript
{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "dailyLimitExceededUnreg",
    "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
    "extendedHelp": "https://code.google.com/apis/console"
   }
  ],
  "code": 403,
  "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
 }
}

Do something like this:
PHP
if( isset( $ytdata['error'] ) ) {
  // something wrong...
}
else {
  // go on...
}

CommentThreads: list  |  YouTube Data API  |  Google Developers[^]
 
Share this answer
 
Thanks @Kornfeld Eliyahu Peter

this part of the code is working fine

PHP
foreach ($ytdata->items as $item) {
    echo "Author: ". $item->snippet->topLevelComment->snippet->authorDisplayName;
    echo "Comment: ". $item->snippet->topLevelComment->snippet->textDisplay;
}


but when I add a foreach inside foreach that's the time I'm getting an error result
 
Share this answer
 
Comments
Richard Deeming 17-Aug-17 10:28am    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution.

DO NOT post your comment as a "solution".
Bernie Carpio 1-Sep-17 20:56pm    
Ok Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900