使用 jQuery 获取 Youtube 评论

发布于 2024-12-23 14:04:39 字数 2101 浏览 2 评论 0原文

我有一个客户项目,该项目将使用 YouTube 评论流作为一种“聊天”。我正在使用 GData API 和 Zend Framework 对 YouTube 进行经过身份验证的调用。我正在寻找一种运行脚本的方法,该脚本将使用刷新按钮拉取评论流,以便用户不必刷新页面即可查看他们的评论或出现的任何新评论。我相信这可以通过 JQuery 来完成,但经过一番搜索后,我还没有真正找到关于如何实现的很好的解释。以下是我的代码的一些简短片段,可让您了解我正在查看的内容:

$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);
$_SESSION['yt'] = serialize($yt);

/***************** Adds a comment if applicable *****************/

if(isset($_POST['btn_submit']))
{
$videoEntry = $yt->getVideoEntry('QQoFLrZ5C3M');

$newComment = $yt->newCommentEntry();
$newComment->content = $yt->newContent()->setText($_POST['comment']);

// post the comment to the comments feed URL for the video
$commentFeedPostUrl = $videoEntry->getVideoCommentFeedUrl();
$updatedVideoEntry = $yt->insertEntry($newComment, $commentFeedPostUrl,
'Zend_Gdata_YouTube_CommentEntry');
}

                        /****************************************************************/


<div id="coments">

$commentFeed = $yt->getVideoCommentFeed('QQoFLrZ5C3M');

echo '<div id="refresh"><a href="#" style="margin-right: 15px; border: 0;"><img src="../common/img/refresh.png" alt="refresh" border="0" /></a></div>';

foreach ($commentFeed as $commentEntry) {
echo '<div class="comment">';
echo  '<a href="http://youtube.com/user/' . utf8_decode(utf8_encode($commentEntry->author[0]->name->text)) . '" target="_blank" class="youtube_user">' . utf8_decode(utf8_encode($commentEntry->author[0]->name->text)) . '</a><br />';

echo  '<span style="font-size: 14px;">' . utf8_decode(utf8_encode($commentEntry->content->text)) . '</span><br />';

// Format time
$timeAgoObject = new convertToAgo;
$ts = strtotime($commentEntry->updated->text);
$timestamp = ($timeAgoObject -> makeAgo($ts)); // Then convert to ago time

echo  '<div class="yt_timestamp">' . $timestamp  . '</div>';
echo '</div>';
}
?>
</div>

请注意,youtube 类并不总是新的(因此序列化为会话变量)我只是删除了 if 语句以便于阅读。

I have a project for a client that will be using a YouTube comment stream as a type of "chat." I am making an authenticated call to YouTube using the GData APIs with the Zend Framework. I am looking for a way to run the script that will pull the comment stream with a refresh button so that the users don't have to refresh the page to see their comment, or any new comments that appear. I believe this can be accomplished with JQuery, but after a bt of searching I haven't really found a good explanation as to how. Here are a few brief snippits of my code to give you some idea of what I am looking at:

$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);
$_SESSION['yt'] = serialize($yt);

/***************** Adds a comment if applicable *****************/

if(isset($_POST['btn_submit']))
{
$videoEntry = $yt->getVideoEntry('QQoFLrZ5C3M');

$newComment = $yt->newCommentEntry();
$newComment->content = $yt->newContent()->setText($_POST['comment']);

// post the comment to the comments feed URL for the video
$commentFeedPostUrl = $videoEntry->getVideoCommentFeedUrl();
$updatedVideoEntry = $yt->insertEntry($newComment, $commentFeedPostUrl,
'Zend_Gdata_YouTube_CommentEntry');
}

                        /****************************************************************/


<div id="coments">

$commentFeed = $yt->getVideoCommentFeed('QQoFLrZ5C3M');

echo '<div id="refresh"><a href="#" style="margin-right: 15px; border: 0;"><img src="../common/img/refresh.png" alt="refresh" border="0" /></a></div>';

foreach ($commentFeed as $commentEntry) {
echo '<div class="comment">';
echo  '<a href="http://youtube.com/user/' . utf8_decode(utf8_encode($commentEntry->author[0]->name->text)) . '" target="_blank" class="youtube_user">' . utf8_decode(utf8_encode($commentEntry->author[0]->name->text)) . '</a><br />';

echo  '<span style="font-size: 14px;">' . utf8_decode(utf8_encode($commentEntry->content->text)) . '</span><br />';

// Format time
$timeAgoObject = new convertToAgo;
$ts = strtotime($commentEntry->updated->text);
$timestamp = ($timeAgoObject -> makeAgo($ts)); // Then convert to ago time

echo  '<div class="yt_timestamp">' . $timestamp  . '</div>';
echo '</div>';
}
?>
</div>

Note that the youtube class is not always new (hence the serialization into a session variable) I just stripped out the if statement for easy reading.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

開玄 2024-12-30 14:04:39

基本思想如下:

  1. 向用户呈现评论表单
  2. 使用 jQuery 挂钩表单的提交事件。 (docs)
  3. 通过 AJAX 将表单提交到后端脚本。 (docs)
  4. 照常将记录插入数据库。
  5. 回显新评论的 HTML。
  6. 请求完成后,jQuery 将返回您回显的任何 HTML。剩下的就是将新的 HTML 添加到您的评论列表中。 (文档

Here's the basic idea:

  1. Present the comment form to the user
  2. Use jQuery to hook the form's submit event. (docs)
  3. Submit the form via AJAX to your backend script. (docs)
  4. Insert the record into your database as normal.
  5. Echo the HTML for the new comment.
  6. After the request is complete, jQuery will return whatever HTML you've echo'd. All that's left is to prepend/prepend the new HTML to your comment list. (docs)
揽清风入怀 2024-12-30 14:04:39

我创建了一个示例供您使用。它利用了 Youtube JSON API,详细信息如下:http://code.google .com/apis/youtube/2.0/reference.html#Comments_Feeds

我使用的是 JSON,而不是 XML。

要使用 JSON 获取视频的“数据”,请使用 http://gdata.youtube.com/feeds/api/videos/YOURVIDEOID/comments

示例:
http://jsfiddle.net/A32H2/2/

//"The Dark Knight Rises trailer, sweded" comments
//http://www.youtube.com/watch?v=KrmEyPkgDf8

    <div id="comments"></div>

    $.ajax({
        url: "http://gdata.youtube.com/feeds/api/videos/KrmEyPkgDf8/comments?v=2&alt=json&max-results=50",  
        //gets the max first 50 results.  To get the 'next' 50, use &start-index=50
        dataType: "jsonp",
        success: function(data){
            $.each(data.feed.entry, function(key, val) {

                comment = $("<div class='comment'></div>");

                author = $("<a target='_blank' class='youtube_user'></a>");
                author.attr("href", "http://youtube.com/user/" + val.author[0].name.$t);
                author.html(val.author[0].name.$t);

                content = $("<div style='font-size: 14px;' class='content'></div>");
                content.html(val.content.$t);

                comment.append(author).append(content);
                $('#comments').append(comment);
            });
        }
    });

另外我建议更改您的问题的标题更具描述性的内容,例如“使用 jQuery 获取 Youtube 评论”

I created an example for you to use. It utilizes the Youtube JSON API somewhat detailed here: http://code.google.com/apis/youtube/2.0/reference.html#Comments_Feeds

Insead of XML I am using JSON.

To get the 'data' of your video with JSON, use http://gdata.youtube.com/feeds/api/videos/YOURVIDEOID/comments

Example:
http://jsfiddle.net/A32H2/2/

//"The Dark Knight Rises trailer, sweded" comments
//http://www.youtube.com/watch?v=KrmEyPkgDf8

    <div id="comments"></div>

    $.ajax({
        url: "http://gdata.youtube.com/feeds/api/videos/KrmEyPkgDf8/comments?v=2&alt=json&max-results=50",  
        //gets the max first 50 results.  To get the 'next' 50, use &start-index=50
        dataType: "jsonp",
        success: function(data){
            $.each(data.feed.entry, function(key, val) {

                comment = $("<div class='comment'></div>");

                author = $("<a target='_blank' class='youtube_user'></a>");
                author.attr("href", "http://youtube.com/user/" + val.author[0].name.$t);
                author.html(val.author[0].name.$t);

                content = $("<div style='font-size: 14px;' class='content'></div>");
                content.html(val.content.$t);

                comment.append(author).append(content);
                $('#comments').append(comment);
            });
        }
    });

Also I would recommend changing the title of your question to something more descriptive, such as "Get Youtube comments with jQuery"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文