我正在使用Simplepie将YouTube RSS Feed导入我的网站。我有一切正常工作,但我不能为我的生活弄清楚如何拉入每个特定视频的视频视图 - 我可以得到视频标题,链接和日期,但我找不到任何方式显示该特定视频的视图。我不确定是否可能。
由于
我目前使用的代码是:
<?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>
答案 0 :(得分:1)
如果有人卡住这对我有用......虽然我确定有一个更清洁的&#34;这样做的方式:
<?php
$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;
?>