【发布时间】:2014-05-12 13:03:02
【问题描述】:
我正在使用 Simplepie 将 YouTube RSS 提要拉入我的网站。我的一切工作正常,但是我一生无法弄清楚如何为每个特定视频提取视频视图 - 我可以获得视频标题、链接和日期,但我找不到任何方法显示该特定视频的观看次数。我实际上不确定这是否可能。
谢谢
到目前为止我工作的代码是:
<?php // Get RSS Feed(s)
// Get a SimplePie feed object from the specified feed source.
$rss2 = fetch_feed('http://gdata.youtube.com/feeds/base/users/SCREENAME/uploads?alt=rss&v=2&orderby=published&client=ytapi-youtube-profile');
if (!is_wp_error( $rss2 ) ) : // Checks that the object is created correctly
// Figure out how many total items there are, but limit it to 5.
$maxitems = $rss2->get_item_quantity(2);
// Build an array of all the items, starting with element 0 (first element).
$rss_items = $rss2->get_items(0, $maxitems);
endif;
?>
<?php $placeholders = array(' ', '/', ',', '&', '?', '.', '’', ''', ':');?>
<?php $vals = array('_', '', '', '', '', '', '', '', '');?>
<?php $placeholdersFeed = array('watch?v=', 'feature=youtube_gdata', '&', '&', '&');?>
<?php $valsFeed = array('embed/', '', '', '', '');?>
<ul>
<?php if ($maxitems == 0) echo '<li>No items.</li>';
else
// Loop through each feed item and display each item as a hyperlink.
foreach ( $rss_items as $item ) : ?>
<li>
<a class="various fancybox.iframe" href='<?php echo esc_url( ( str_replace( $placeholdersFeed, $valsFeed, $item->get_permalink() ))); ?>?autoplay=1' title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'><?php echo esc_html( $item->get_title() ); ?></a>
</li>
<?php endforeach; ?>
</ul>
【问题讨论】:
-
你点击的实际 rss url 是什么。
-
万一有人被卡住,这对我有用......虽然我确信有一种“更清洁”的方式:
-
$url2 = $item->get_permalink(); if ( preg_match('![?&]{1}v=([^&]+)!', $url2 . '&', $m2 )) $id2 = $m2[1]; $video_ID = $id2; $JSON = file_get_contents("https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json"); $JSON_Data = json_decode($JSON); $views = $JSON_Data->{'entry'}->{'yt$statistics'}->{'viewCount'}; echo $views; -
@Darren - 这看起来应该是一个答案,为什么不把它作为一个答案发布?
标签: video youtube rss views simplepie