Example #1
0
 /**
  * @param string $startUrl
  * @param string $filePath
  * @throws GeneratorException
  */
 public function write($startUrl, $filePath)
 {
     $storage = $this->storage;
     $zip = new \ZipArchive();
     if ($zip->open($filePath, \ZipArchive::CREATE) !== true) {
         throw new GeneratorException('Can not open archive');
     }
     $offset = 0;
     $limit = 500;
     $now = date('Y-m-d');
     $this->currentCollection = new Collection();
     $i = 0;
     while (true) {
         $storage->setOffset($offset);
         $storage->setLimit($limit);
         $links = $storage->get();
         if (empty($links)) {
             $this->saveCollection($zip, $i);
             break;
         }
         foreach ($links as $url) {
             $this->currentNum++;
             $map = new SitemapEntry($url->url);
             $map->setPriority(round(1 / $url->level, 2));
             $map->setChangeFreq(SitemapEntry::CHANGEFREQ_ALWAYS);
             $map->setLastMod($now);
             if ($this->currentNum == $this->maxLinksPerFile) {
                 $this->saveCollection($zip, $i);
             }
             $this->currentCollection->addSitemap($map);
         }
         $offset += $limit;
     }
     $indexCollection = new Collection();
     $indexCollection->setFormatter(new SitemapIndex());
     $baseUrl = rtrim($startUrl, chr('/')) . '/';
     foreach ($this->index as $fileName) {
         $map = new SitemapEntry($baseUrl . $fileName);
         $map->setLastMod($now);
         $indexCollection->addSitemap($map);
     }
     $zip->addFromString("sitemap-index.xml", $indexCollection->output());
     if ($zip->close()) {
         $this->trigger(self::EVENT_FINISH);
     } else {
         throw new GeneratorException('Cant save sitemap archive');
     }
 }
Example #2
0
// Your base url for the mediabank application /detail/{id} is the route for the permalink
// your application would be installed under http://demo.webservices.picturae.pro/mediabank/
$baseURL = 'http://demo.webservices.picturae.pro/mediabank/detail/';
$client = new Client($apiKey);
// This part should be cached to avoid the extra request
$rows = 100;
$result = $client->search(['rows' => $rows]);
$pages = $result->metadata->pagination->pages;
$currentPage = null;
if (isset($_GET['page'])) {
    $currentPage = (int) $_GET['page'];
}
$collection = new Collection();
if ($currentPage) {
    // Render the sitemap for the current page
    $result = $client->search(['rows' => $rows, 'page' => $currentPage]);
    foreach ($result->media as $media) {
        $basic = new SitemapEntry($baseURL . $media->id);
        $collection->addSitemap($basic);
    }
} else {
    // Render the sitemap with all other sitemap
    for ($index = 0; $index < $pages; $index++) {
        $basic = new SitemapEntry($url->getCurrentURL() . '?page=' . ($index + 1));
        $collection->addSitemap($basic);
    }
}
$collection->setFormatter(new URLSet());
$collection->setFormatter(new SitemapIndex());
header("Content-type: text/xml; charset=utf-8");
echo $collection->output();