Update existing function for YouTube API 3.0 from 2.0
-
I am working on a website, and there was a function that would display a list of the users videos. Now I am just getting this error:
Warning: file_get_contents(https://www.youtube.com/feeds/videos.xml?channel_id=UCqWTiyd9_A6oAOKHJkDOlZQk) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in /home1/USERNAME/public_html/wp-content/themes/mythemehere/functions.php on line 570
Now here is the included function where the error originates as follows:
function fetch_youtube_rss($cache_len = 3600) { $tubes = get_transient('youtube_rss_feed'); if (!$tubes) { $url = 'https://gdata.youtube.com/feeds/base/users/themidgetgrimm/uploads?alt=rss'; if ($contents = file_get_contents($url)) { $xml = simplexml_load_string($contents); $tubes = array(); foreach($xml->channel->item as $item) { $tubes[] = @json_decode(@json_encode($item)); } } else { return false; } set_transient('youtube_rss_feed', $tubes, $cache_len); } return $tubes; }
And here is the php on the page for which I am calling it:
$tubes = fetch_youtube_rss(); //this is at top of page //then where issue is called //keep in mind I wrote none of this, just trying to fix it <?php $first_vid = $tubes[0]; if (preg_match('/(?<=v(\=|\/))([-a-zA-Z0-9_]+)|(?<=youtu\.be\/)([-a-zA-Z0-9_]+)/',$first_vid->link,$matches)) : $first_id = $matches[0]; ?> <iframe width="295" height="196" src="https://www.youtube.com/embed/<?php echo $first_id ?>?rel=0" frameborder="0" allowfullscreen></iframe> <?php endif; ?> <h3>More Videos:</h3> <ul style="list-style:none; margin:0; padding:0;"> <?php for($i = 1; $i < 19; $i++) : $vid_id = 'unknown'; if (preg_match('/(?<=v(\=|\/))([-a-zA-Z0-9_]+)|(?<=youtu\.be\/)([-a-zA-Z0-9_]+)/',$tubes[$i]->link,$matches)) { $vid_id = $matches[0]; } ?> <li class="clearfix" style="margin:0 0 5px 0; padding:0;"> <div class="vid_thumb" style="width:50px; height:50px; display:block; float:left;"> <a href="<?php echo esc_attr($tubes[$i]->link) ?>" target="_blank"><img src="https://i.ytimg.com/vi/<?php echo $vid_id ?>/0.jpg" alt="" style="width:40px; height:30px; float:left;"/></a> </div> <div class="vid_info" style="width:245px; display:block; float:left;"> <h4 style="margin-bottom:0;"><a href="<?php echo esc_attr($tubes[$i]->link) ?>" target="_blank"><?php echo esc_attr($tubes[$i]->title) ?></a></h4> <p class="meta" style="font-size:0.8em; color:#666;">Posted <?php echo dt_format($tubes[$i]->pubDate, 'm.d.Y') ?></p> </div> </li> <?php endfor; ?> </ul> </div>
From what I have read is that YouTube has updated their API from 2 to 3 and have completely shutdown 2. So any help to point me in the right direction. I have searched on here, but they seem to all be tied to specific plugins, not just a random bit of custom code. Any and all help is appreciated.. I have tried some alterations, but to no avail. Please help…
- The topic ‘Update existing function for YouTube API 3.0 from 2.0’ is closed to new replies.