Home  >  Article  >  Backend Development  >  php automatically generates sitemap

php automatically generates sitemap

angryTom
angryTomforward
2019-10-15 13:17:475950browse

php automatically generates sitemap

The sitemap of the website is the main place for crawlers to crawl. If you submit the URL of the sitemap, crawlers will crawl it first.

So we must have the habit of updating sitemap regularly. There are many ways to generate sitemap. Third-party tools can capture and generate it, and you can generate it yourself. Here I recommend generating the sitemap yourself. Because this consumes the least system resources.

The code is as follows:

include_once Root_Path . "/vendor/autoload.php";
use Medoo\Medoo;
$db = new Medoo([
'database_type' => 'mysql',
'database_name' => 'menghuiguli',
'server' => 'localhost',
'username' => 'root',
'password' => 'root',
]);
$str = "<ul>";
$articles = $db->select(&#39;article&#39;,"*");
foreach ($articles as $k => $v) {
$str .= &#39;<li>
<div class="T1"><a href="https://www.80shihua.com/archives/&#39;.$v[&#39;id&#39;].&#39;" title="&#39;.$v[&#39;name&#39;].&#39;">&#39;.$v[&#39;name&#39;].&#39;</a></div>
<div class="T2">2019-02-02T10:16:23+08:00</div>
<div class="T3">monthly</div>
<div class="T4">0.5</div>
</li>&#39;;
}
$str .= &#39;</ul>&#39;;
file_put_contents(&#39;/sitemap.html&#39;, $str);

I used the smallest medooORM model for the database, which is very convenient.

We only need to find relevant articles, and then generate a sitemap in a corresponding loop.

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of php automatically generates sitemap. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:www.80shihua.com. If there is any infringement, please contact admin@php.cn delete