Atom::generate PHP Method

generate() public method

输出字符串
public generate ( ) : string
return string
    public function generate()
    {
        $result = '<?xml version="1.0" encoding="' . $this->_charset . '"?>' . self::EOL;
        $result .= '<feed xmlns="http://www.w3.org/2005/Atom"
xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>' . self::EOL;
        $content = '';
        $lastUpdate = 0;
        foreach ($this->_items as $item) {
            $item['updated'] = $item['updated'] > 0 ? $item['updated'] : $item['published'];
            $content .= '<entry>' . self::EOL;
            $content .= '<title type="text">' . $this->encode($item['title']) . '</title>' . self::EOL;
            $content .= '<link rel="alternate" type="text/html" href="' . $item['link'] . '" />' . self::EOL;
            $content .= '<id>' . (isset($item['id']) ? $item['id'] : $item['link']) . '</id>' . self::EOL;
            $content .= '<updated>' . date('c', $item['updated']) . '</updated>' . self::EOL;
            $content .= '<published>' . date('c', $item['published']) . '</published>' . self::EOL;
            if (!empty($item['author'])) {
                $content .= '<author>
<name>' . $item['author']['name'] . '</name>
<uri>' . $item['author']['url'] . '</uri>
</author>' . self::EOL;
            }
            if (!empty($item['category']) && is_array($item['category'])) {
                foreach ($item['category'] as $category) {
                    $content .= '<category scheme="' . $category['feeds_url'] . '" term="' . $category['name'] . '" />' . self::EOL;
                }
            }
            if (!empty($item['content'])) {
                $content .= '<summary type="html">' . $this->encode($item['content']) . '</summary>' . self::EOL;
            }
            $content .= '</entry>' . self::EOL;
            if ($item['updated'] > $lastUpdate) {
                $lastUpdate = $item['updated'];
            }
        }
        $result .= '<title type="text">' . $this->encode($this->_title) . '</title>
<subtitle type="text">' . $this->encode($this->_subTitle) . '</subtitle>
<updated>' . date('c', $lastUpdate) . '</updated>
<link rel="alternate" type="text/html" href="' . $this->_baseUrl . '" />
<link rel="self" type="application/atom+xml" href="' . $this->_feedUrl . '" />
<id>' . $this->_feedUrl . '</id>
<creativeCommons:license>http://www.creativecommons.org/licenses/by-sa/2.5/rdf</creativeCommons:license>
' . $content . '</feed>';
        return $result;
    }

Usage Example

Exemplo n.º 1
0
            if ('archive' != $type && !empty($post[$type])) {
                foreach ($post[$type] as $meta) {
                    $item['category'][] = ['feeds_url' => $meta['url'], 'name' => $meta['name']];
                }
            }
        }
        $feeds->addItem($item);
    }
    $target = $context->dir . '_target/' . $config['target'];
    $targetDir = dirname($target);
    if (!is_dir($targetDir)) {
        if (!mkdir($targetDir, 0755, true)) {
            le_fatal('feeds directory is not exists "%s"', $targetDir);
        }
    }
    file_put_contents($target, $feeds->generate());
});
// generate sitemap
le_add_workflow('generate_sitemap', function () use($context) {
    $fp = fopen($context->dir . '_target/sitemap.xml', 'wb');
    if (!$fp) {
        le_fatal('can not write sitemap.xml');
    }
    $base = isset($context->data['url']) ? rtrim($context->data['url'], '/') : '/';
    fwrite($fp, '<?xml version="1.0" encoding="UTF-8"?>
<urlset
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">');
    foreach ($context->sitemap as $url => $priority) {