Skip to content

Instantly share code, notes, and snippets.

@hannit
Created December 29, 2016 11:47
Show Gist options
  • Save hannit/1b0a594f6cf966397dd91b7b5cb89e32 to your computer and use it in GitHub Desktop.
Save hannit/1b0a594f6cf966397dd91b7b5cb89e32 to your computer and use it in GitHub Desktop.
function get_videos_from_youtube_api($num) {
if ( false === ( $data = get_transient( 'grafa-youtube-result' ) ) ) {
$key = 'Youtube-Key';
$channel = 'Channel-ID';
// $url = "https://www.googleapis.com/youtube/v3/playlistItems?key=$key&playlistId=$channel&part=snippet,id&order=date&maxResults=20";
$url = "https://www.googleapis.com/youtube/v3/search?key=$key&channelId=$channel&part=snippet,id&order=date&maxResults=20";
$data = file_get_contents($url);
set_transient( 'grafa-youtube-result', $data, 4 * HOUR_IN_SECONDS );
}
if (!empty($data)) {
echo "<div class='row'>";
$data_struct = json_decode($data);
$i = 0;
for ($i = 0 ; $i < $num; $i++) {
$y = $data_struct->items[$i];
$img = $y->snippet->thumbnails->medium->url;
$vid_id = $y->id->videoId;
$title = $y->snippet->title;
?>
<div class='col-md-4'>
<div class='y-movie youtube'>
<div class='entry-thumbnail'>
<a class='image-link' href='<?php echo "http://www.youtube.com/embed/".$vid_id."?rel=0&wmode=transparent";?>'>
<img src='<?php echo $img;?>' />
</a>
</div>
<h4 class='grid-heading'>
<a href='<?php echo "http://www.youtube.com/embed/".$vid_id."?rel=0&wmode=transparent";?>'>
<?php echo $title; ?>
</a>
</h4>
</div>
</div>
<?php
}
echo "</div>";
echo '<script type="text/javascript">';
echo 'jQuery(document).ready(function($) {';
echo '$(".youtube a").colorbox({iframe:true, innerWidth:853, innerHeight:480});';
echo '});';
echo '</script>';
} else
echo "Empty data recieved";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment