使用 jQuery 获取 Youtube 评论
我有一个客户项目,该项目将使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本思想如下:
Here's the basic idea:
我创建了一个示例供您使用。它利用了 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/
另外我建议更改您的问题的标题更具描述性的内容,例如“使用 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/
Also I would recommend changing the title of your question to something more descriptive, such as "Get Youtube comments with jQuery"