Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save co3k/158169 to your computer and use it in GitHub Desktop.
Save co3k/158169 to your computer and use it in GitHub Desktop.
<?php
define('SNS_TITLE', 'MySNS');
define('FEED_URL', 'http://example.com/api.php/feeds/diary');
define('APPEND_CDATA_TO_CONTENT', true);
// ---
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);
$ch = curl_init(FEED_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xmlString = curl_exec($ch);
curl_close($ch);
if (!$xmlString)
{
header('HTTP/1.0 404 Not Found');
exit;
}
header('Cache-Control: private, max-age='.(60*30));
if (APPEND_CDATA_TO_CONTENT)
{
$xmlString = _appendCDataToContent($xmlString);
}
$data = array(
'title' => SNS_TITLE.'の日記',
'link' => FEED_URL,
'charset' => 'UTF-8',
'entries' => array(),
);
$xml = simplexml_load_string($xmlString, 'SimpleXMLElement', LIBXML_NOCDATA);
foreach ($xml->entry as $entry)
{
$data['entries'][] = array(
'title' => mb_strimwidth((string)$entry->title, 0, 150, '', 'UTF-8'),
'link' => (string)$entry->link[0]['href'],
'description' => mb_strimwidth((string)$entry->content, 0, 300, '...', 'UTF-8'),
'lastUpdate' => strtotime((string)$entry->published),
'author' => (string)$entry->author->name,
);
}
$rss = Zend_Feed::importArray($data, 'rss');
$rss->send();
function _appendCDataToContent($string)
{
$search = array('<content>', '<content type="text">', '</content>');
$replace = array('<content><![CDATA[', '<content type="text"><![CDATA[', ']]></content>');
return str_replace($search, $replace, $string);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment