Home > Article > Backend Development > 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('article',"*"); foreach ($articles as $k => $v) { $str .= '<li> <div class="T1"><a href="https://www.80shihua.com/archives/'.$v['id'].'" title="'.$v['name'].'">'.$v['name'].'</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>'; } $str .= '</ul>'; file_put_contents('/sitemap.html', $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!