Last active
August 31, 2022 06:03
-
-
Save mklooss/3be4fce95c8e109cd29023833cc7c6e7 to your computer and use it in GitHub Desktop.
Warmup from sitemap xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
exit; // change url first | |
$mainXmlUrl = 'https://www.example.com/sitemap.xml'; | |
$mainXmlContent = curlUrlExec($mainXmlUrl); | |
$mainXml = @simplexml_load_string($mainXmlContent); | |
unset($mainXmlContent); | |
$urls = []; | |
// check if its an normal sitemap xml | |
if (isset($mainXml->url)) | |
{ | |
$urls[] = $mainXmlUrl; | |
} | |
// check if its an sitemapindex xml | |
if (isset($mainXml->sitemap)) | |
{ | |
foreach ($mainXml->sitemap as $sitemap) | |
{ | |
$urls[] = (string)$sitemap->loc; | |
} | |
} | |
unset($mainXml); | |
// parse XML and URLs :) | |
foreach ($urls as $url) { | |
logLine("URL: $url"); | |
$xmlContent = curlUrlExec($url); | |
$xml = @simplexml_load_string($xmlContent, 'SimpleXMLElement', LIBXML_COMPACT | LIBXML_PARSEHUGE); | |
unset($xmlContent); // free memory before | |
if (!isset($xml->url)) | |
{ | |
continue; | |
} | |
foreach ($xml->url as $value) | |
{ | |
logLine($value->loc, $url); | |
curlUrlExec((string)$value->loc); | |
logLine('DONE: '.$value->loc, $url); | |
} | |
unset($xml); // free memory before | |
} | |
/** | |
* | |
* @param string $msg | |
*/ | |
function logLine($msg, $part = 'default') | |
{ | |
$part = basename((string)$part); | |
echo date('r').': ['.(string)$part.'] '.(string)$msg."\n"; | |
} | |
/** | |
* | |
* @param string $url | |
* @return type | |
*/ | |
function curlUrlExec($url) | |
{ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, trim($url)); | |
curl_setopt($ch, CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 10); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
if (pathinfo($url, PATHINFO_EXTENSION) == 'gz') | |
{ | |
$result = gzdecode($result); | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment