Skip to content

Instantly share code, notes, and snippets.

@Skalman
Created April 20, 2015 23:48
Show Gist options
  • Save Skalman/801436d9693ff03bc4ce to your computer and use it in GitHub Desktop.
Save Skalman/801436d9693ff03bc4ce to your computer and use it in GitHub Desktop.
Dynamic RSS feeds from Youtube links
<?php
if (!isset($_GET['url'])) {
?>
<!DOCTYPE html>
<meta charset="utf-8">
<title>Youtube RSS creator</title>
<form>
<p>Create an RSS feed for the videos on the following page:
<p><input name="url" placeholder="E.g. https://www.youtube.com/user/scishow/videos" style="width: 30em">
<p><input type="submit" value="Create">
</form>
<?php
exit;
}
$url = $_GET['url'];
$host = preg_replace('#^(https?://[^/]+).*#', '$1', $url);
$author = preg_replace('#^(https?://[^/])/user/([^/]+).*#', '$1', $url);
$html = file_get_contents($url);
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXpath($doc);
$mainTitle = $xpath->query('//title')->item(0)->nodeValue;
$links = $xpath->query('//a[starts-with(@href, "/watch")]');
$entries = [];
if (!is_null($links)) {
foreach ($links as $link) {
$href = $link->getAttribute('href');
$title = $link->getAttribute('title');
if ($title === '')
$title = trim($link->nodeValue);
if (!isset($entries[$href]) || strlen($entries[$href]) < strlen($title))
$entries[$href] = $title;
}
}
header('Content-Type: application/atom+xml; charset=utf-8');
$mainTitle = htmlspecialchars($mainTitle);
$protocol = $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
$port = ":$_SERVER[SERVER_PORT]";
if (($protocol === 'http' && $port === ":80") || ($protocol === 'https' && $port === ":443"))
$port = '';
$self = htmlspecialchars("$protocol://$_SERVER[SERVER_NAME]$port$_SERVER[REQUEST_URI]");
$url = htmlspecialchars($url);
$author = htmlspecialchars($author);
$updated = time();
$updated -= $updated % 60;
?>
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title type="text"><?= $mainTitle ?></title>
<link rel="self" href="<?= $self ?>" type="application/atom+xml" />
<link rel="alternate" href="<?= $url ?>" type="text/html" />
<updated><?= date('c', $updated) ?></updated>
<id><?= $url ?></id>
<?php
foreach ($entries as $entryUrl => $title) {
$entryUrl = htmlspecialchars("$host$entryUrl");
$title = htmlspecialchars($title);
$updated -= 60;
?>
<entry>
<id><?= $entryUrl ?></id>
<title type="text"><?= $title ?></title>
<link rel="alternate" href="<?= $entryUrl ?>" />
<author>
<name><?= $author ?></name>
</author>
<updated><?= date('c', $updated) ?></updated>
</entry>
<?php
}
?>
</feed>
@jassishekhawat
Copy link

I got this error
Parse error: syntax error, unexpected '[' in D:\xampp\htdocs\youtubehd2.3.1\test.php on line 31

and if remove line 31 then

Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: ID watch-like already defined in Entity, line: 480 in D:\xampp\htdocs\youtubehd2.3.1\test.php on line 24

Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: ID watch-dislike already defined in Entity, line: 494 in D:\xampp\htdocs\youtubehd2.3.1\test.php on line 24

Warning: Cannot modify header information - headers already sent by (output started at D:\xampp\htdocs\youtubehd2.3.1\test.php:24) in D:\xampp\htdocs\youtubehd2.3.1\test.php on line 44

@ongpang
Copy link

ongpang commented Dec 26, 2015

$channel_id = 'XXX'; // put the channel id here
$youtube = file_get_contents('https://www.youtube.com/feeds/videos.xml?channel_id='.$channel_id);
$xml = simplexml_load_string($youtube, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$youtube = json_decode($json, true);
$yt_vids = array();
$count = 0;
foreach ($youtube['entry'] as $k => $v) {
$yt_vids[$count]['id'] = str_replace('http://www.youtube.com/watch?v=', '', $v['link']['@attributes']['href']);
$yt_vids[$count]['title'] = $v['title'];
$count++;
}
print_r($yt_vids);

@thejustsoul
Copy link

Channel names which in Russian (Cyrillic) language (including video titles) are displayed in characters not readable.
Correct encoding if possible..
example: Т�ейле� к канал�. Эк�н видео � �Ш!

@bay122
Copy link

bay122 commented Feb 2, 2016

have a problem with:
Parse error: syntax error, unexpected 'version' (T_STRING) in /opt/lampp/htdocs/yii-basic/web/index.php on line 47

@melice
Copy link

melice commented Aug 5, 2016

@bay122 removed the "?" at line 47

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment