Skip to content

Instantly share code, notes, and snippets.

@ismoil-nosr
Last active December 21, 2019 22:55
Show Gist options
  • Save ismoil-nosr/3d15ff0b795d6c5c094ea76eb2b79991 to your computer and use it in GitHub Desktop.
Save ismoil-nosr/3d15ff0b795d6c5c094ea76eb2b79991 to your computer and use it in GitHub Desktop.
The script parses youtube channel and gets latest videos
<?php
/*
@Author: https://github.com/divinity76
*/
$channel = 'https://www.youtube.com/user/Tobuscus/videos?view=0&sort=dd&flow=grid'; //link to channel with sort parametrs in url
$maxResults = 3; //max number of videos to parse/output
$html = file_get_contents($channel);
$domd = @DOMDocument::loadHTML($html);
$xp = new DOMXPath($domd);
$videoList = $xp->query("//li/ul/li[contains(@class,'channels-content-item')]");
$parsed = array();
for ($i = 0; $i < $maxResults; ++$i) {
$video = $videoList[$i];
$title = trim($xp->query(".//*[contains(@class,'yt-lockup-title')]/a", $video)->item(0)->textContent);
$id = $xp->query(".//*[@data-context-item-id]", $video)->item(0)->getAttribute("data-context-item-id");
$parsed[$id] = $title;
}
//var_dump($parsed);
/* Output videos */
foreach($parsed as $key => $value){
echo '<div class="video">
<iframe width="100%" class="youtube-video" src="https://www.youtube.com/embed/'.$key.'" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment