Created
October 5, 2016 16:21
-
-
Save dubrod/885ddb70ab13750fc64a7fc2119aeeca to your computer and use it in GitHub Desktop.
Pass a you Video ID or comma separated string of IDS to output a thumbnail and title and a link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//chunk youtube-video-tpl | |
<div class="col-1-3"> | |
<a class="mediabox" rel="ytv" href="http://www.youtube.com/embed/[[+id]]"> | |
<img src="[[+thumb]]" alt=""> | |
<h3>[[+title]]</h3> | |
</a> | |
</div> | |
//Snippet | |
<?php | |
/* YouTube Video Feed FEED | |
* Wayne Roddy ( [email protected] ) | |
* Sept. 2016 | |
* I'm not a PHP developer. No laughing | |
* You need a YouTube API Key from the Google Console | |
* [[YouTubeVids? &videos=``]] | |
*/ | |
$output = ""; | |
$input = $modx->getOption('videos', $scriptProperties); | |
if(!input){ | |
return "No Videos Given."; | |
} | |
$YTSarray = explode(",", $input); | |
foreach($YTSarray as $v){ | |
$key = $modx->getOption('youtube_key'); | |
$url = "https://www.googleapis.com/youtube/v3/videos?id=".$v."&part=snippet&key=".$key.""; | |
$ch = curl_init(); | |
$timeout = 5; | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); | |
$data = curl_exec($ch); | |
curl_close($ch); | |
$ytvs = json_decode($data); | |
//print_r($ytvs->items); | |
/* | |
Array ( [0] => stdClass Object ( | |
[kind] => youtube#video | |
[etag] => "I_8xdZu766_FSaexEaDXTIfEWc0/r3mAgoEx9f_5OV-815--SUE901E" | |
[id] => C--wx-Wg-Ak | |
[snippet] => stdClass Object ( | |
[publishedAt] => 2013-10-14T15:13:02.000Z | |
[channelId] => UCmixwW6BVjDxovxuBK-PYpw | |
[title] => Underworld | |
[description] => "Underworld" - Billy Floyd : Orlando, FL : Backbooth - Friday, October 4th 2013 : | |
[thumbnails] => stdClass Object ( [default] => stdClass Object ( [url] => https://i.ytimg.com/vi/C--wx-Wg-Ak/default.jpg [width] => 120 [height] => 90 ) [medium] => stdClass Object ( [url] => https://i.ytimg.com/vi/C--wx-Wg-Ak/mqdefault.jpg [width] => 320 [height] => 180 ) [high] => stdClass Object ( [url] => https://i.ytimg.com/vi/C--wx-Wg-Ak/hqdefault.jpg [width] => 480 [height] => 360 ) [standard] => stdClass Object ( [url] => https://i.ytimg.com/vi/C--wx-Wg-Ak/sddefault.jpg [width] => 640 [height] => 480 ) [maxres] => stdClass Object ( [url] => https://i.ytimg.com/vi/C--wx-Wg-Ak/maxresdefault.jpg [width] => 1280 [height] => 720 ) ) [channelTitle] => Billy Floyd [tags] => Array ( [0] => billy floyd [1] => acoustic [2] => original song [3] => Orlando (City/Town/Village) ) [categoryId] => 24 [liveBroadcastContent] => none [localized] => stdClass Object ( [title] => Underworld [description] => "Underworld" - Billy Floyd : Orlando, FL : Backbooth - Friday, October 4th 2013 : ) ) ) ) | |
*/ | |
$items = $ytvs->items; | |
// return $items[0]->id; //C--wx-Wg-Ak | |
$snippet = $ytvs->items[0]->snippet; | |
// return $snippet->title; //Underworld | |
$output .= $modx->parseChunk('youtube-video-tpl', array('id' => $items[0]->id,'title' => $snippet->title,'thumb' => $snippet->thumbnails->high->url)); | |
} | |
return $output; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment