0

I am grabbing the comments from videos, but the "author name" and their id is giving NULL.

This is what I have.

$feedUrl='http://gdata.youtube.com/feeds/api/videos/'.$selected_video_id.'/comments?v=2&alt=json';  
$data = json_decode(file_get_contents($feedUrl),true);
$info = $data["feed"];
$entry = $info["entry"];
$nEntry = count($entry);

for($i=0;$i<$nEntry;$i++){ 
    $name =  $entry[$i]['author']['name']['$t'];
    $userId = $entry[$i]['author']['yt$userId']['$t'];
    $content = $entry[$i]['content']['$t'];
    $published_2 = $entry[$i]['published']['$t'];
}

Content and Published are collected fine, but the name and userID are not.

The feed does have the elements in it as have looked at it in the youtube data api demo beta. As well as it shows everything if you do a feed request in the browser.

http://gdata.youtube.com/feeds/api/videos/VIDEO_ID/comments

So am I missing something?

1 Answer 1

1

It should be:

$name =  $entry[$i]['author'][0]['name']['$t'];
$userId = $entry[$i]['author'][0]['yt$userId']['$t'];
$content = $entry[$i]['content']['$t'];
$published_2 = $entry[$i]['published']['$t'];

Or better yet:

$feedUrl=file_get_contents('http://gdata.youtube.com/feeds/api/videos/ASO_zypdnsQ/comments?v=2&alt=json'); 
$json = json_decode($feedUrl, true);
foreach($json['feed']['entry'] as $entry) {
    echo $entry['author'][0]['name']['$t']."<br>";
    echo $entry['author'][0]['yt$userId']['$t']."<br>";
    echo $entry['content']['$t']."<br>";
    echo $entry['published']['$t']."<br>";
}

You can use foreach like the above :)

Sign up to request clarification or add additional context in comments.

2 Comments

Worked a treat. Thanks. By the way.... Why is the [0] needed in-between when it is not on other items that are nested ?! ( above examples aside)
Apparently youtube allows, or has some sort of availability where each comment has multiple authors. No idea why!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.