Example #1
0
<?php

require_once dirname(__FILE__) . "/../vendor/autoload.php";
use vipnytt\SitemapParser;
use vipnytt\SitemapParser\Exceptions\SitemapParserException;
/**
 * Advanced example
 */
$config = ['guzzle' => []];
try {
    $parser = new SitemapParser('MyCustomUserAgent', $config);
    $parser->parse('https://www.google.com/robots.txt');
    foreach ($parser->getSitemaps() as $url => $tags) {
        echo 'Sitemap<br>';
        echo 'URL: ' . $url . '<br>';
        echo 'LastMod: ' . $tags['lastmod'] . '<br>';
        echo '<hr>';
    }
    foreach ($parser->getURLs() as $url => $tags) {
        echo 'URL: ' . $url . '<br>';
        echo 'LastMod: ' . $tags['lastmod'] . '<br>';
        echo 'ChangeFreq: ' . $tags['changefreq'] . '<br>';
        echo 'Priority: ' . $tags['priority'] . '<br>';
        echo '<hr>';
    }
} catch (SitemapParserException $e) {
    echo $e->getMessage();
}
use vipnytt\SitemapParser;
use vipnytt\SitemapParser\Exceptions\SitemapParserException;
/**
 * Advanced recursive example
 * Full control in every step
 * Supports sitemaps of any number and size
 * Optimized to never run out of memory
 */
$config = ['guzzle' => []];
try {
    $parser = new SitemapParser('MyCustomUserAgent', $config);
    $parser->addToQueue(['https://www.google.com/robots.txt']);
    while (count($queue = $parser->getQueue()) > 0) {
        // Loop through each sitemap individually
        echo '<h3>Parsing sitemap: ' . $queue[0] . '</h3><hr>';
        $parser->parse($queue[0]);
        foreach ($parser->getSitemaps() as $url => $tags) {
            echo 'Sitemap<br>';
            echo 'URL: ' . $url . '<br>';
            echo 'LastMod: ' . $tags['lastmod'] . '<br>';
            echo '<hr>';
        }
        foreach ($parser->getURLs() as $url => $tags) {
            echo 'URL: ' . $url . '<br>';
            echo 'LastMod: ' . $tags['lastmod'] . '<br>';
            echo 'ChangeFreq: ' . $tags['changefreq'] . '<br>';
            echo 'Priority: ' . $tags['priority'] . '<br>';
            echo '<hr>';
        }
    }
} catch (SitemapParserException $e) {
Example #3
0
<?php

require_once dirname(__FILE__) . "/../vendor/autoload.php";
use vipnytt\SitemapParser;
use vipnytt\SitemapParser\Exceptions\SitemapParserException;
/**
 * Basic example
 */
try {
    $parser = new SitemapParser();
    $parser->parse('https://www.google.com/sitemap.xml');
    foreach ($parser->getURLs() as $url => $tags) {
        echo $url . '<br>';
    }
} catch (SitemapParserException $e) {
    echo $e->getMessage();
}
Example #4
0
 * No need to re-download if you already have it
 */
$body = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <sitemap>
    <loc>http://example.com/sitemap02.xml</loc>
  </sitemap>
  <sitemap>
    <loc>http://example.com/sitemap03.xml</loc>
  </sitemap>
  <url>
    <loc>http://example.com/</loc>
  </url>
  <url>
    <loc>http://example.com/about/</loc>
  </url>
</sitemapindex>
XML;
try {
    $parser = new SitemapParser();
    $parser->parse('http://example.com/sitemap.xml', $body);
    foreach ($parser->getSitemaps() as $url => $tags) {
        echo 'Sitemap: ' . $url . '<br>';
    }
    foreach ($parser->getURLs() as $url => $tags) {
        echo $url . '<br>';
    }
} catch (SitemapParserException $e) {
    echo $e->getMessage();
}