php automatically generates sitemap

angryTom
Release: 2023-04-07 16:22:01
forward
6050 people have browsed it

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);
Copy after login

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!

Related labels:
source:www.80shihua.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template