Domanda

I am pulling a YouTube RSS feed into my site using Simplepie. I have everything working correctly, however I can't for the life of me figure out how to pull in the video views for each specific video - I can get the video title, link and date, but I can't find any way of displaying the views for that particular video. I'm not actually sure if it's possible.

Thanks

The code I have working so far is:

<?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(' ', '/', ',', '&amp;', '?', '.', '&#8217;', '&#039;', ':');?>
    <?php $vals = array('_', '', '', '', '', '', '', '', '');?>
    <?php $placeholdersFeed = array('watch?v=', 'feature=youtube_gdata', '&amp;', '&', '&#038;');?>
    <?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>
È stato utile?

Soluzione

In case anyone is stuck this worked for me... Although I'm sure there is a "cleaner" way of doing it:

<?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&al‌​t=json"); $JSON_Data = json_decode($JSON); 
$views = $JSON_Data->{'entry'}->{'yt$statistics'}->{'viewCount'}; 
echo $views;
?>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top