php implementation generates sitemap xml map

This article is from qaq Po Yan blog. https://qagzs.com:88/ Joan article / 43 html

Before submitting sitemap to Google search engine, I found that Google used xml instead of html, so I wanted to use lazy tools to crawl the xml map that generated sitemap on the Internet
Don't ask me why my blog doesn't have an xml map, because the framework functions are written by myself and this function has not been developed, so this xml map is very inconvenient. If you update the article, you have to crawl and upload the xml map again. You plan to write an xml map of sitemap so that the search engine can submit it for collection
The best way of sitemap is to change with the change of content. When the content changes, the sitemap is automatically regenerated
Since you need to generate xml, you need to know the format and specification

sitemap xml format

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://qagzs.com:88/</loc>
<lastmod>2020-08-01T00:00:00+08:00</lastmod>
<changefreq>monthly</changefreq>
<priority>1</priority>
</url>
</urlset>

sitemap xml specification
urlset is used to mark the beginning of the whole document. It is required
url marks the beginning and end of each message. It is required
loc the storage address of this piece of data. The minimum length is 1 character and the maximum length is 256 characters. It starts with http: / / or https: / /. It is required
lastmod refers to the latest update time of the data. The time format is ISO8601, optional
changefreq refers to the update frequency of this piece of data. The valid values are: always, hourly, daily, weekly, monthly, yearly and never. Optional
Priority is used to specify the priority ratio of this link to other links. This value is between 0.0-1.0, optional
sitemap xml requirements
urlset, url and loc are all required. In addition, to prevent garbled code, the XML file needs to be output in utf-8 encoding
Let's talk about another Sitemap file, which contains no more than 50000 web addresses, and the file size should not exceed 10MB. Our new site can be ignored because our site doesn't have so many internal links
PHP implementation
Implement the code according to the above requirements, and then output according to the article records in the database. Here is an example of intercepting the style diagram of my article fields
According to the specification, we need to take out the link, title and time

<?php
  /*
  qaq Po Yan
  Design and production
  Personal introduction
  https://qagzs.com:88/About Joanne/
  Joan studio
  */
  //The output header tells them that this is an xml file
  header('Content-type: text/xml');
  //The output xml file is encoded as UTF-8 and a separate first page priority
  echo '<?xml version="1.0" encoding="UTF-8"?> 
  <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
  <loc>https://qagzs.com:88/</loc>
  <lastmod>2020-08-01T00:00:00+08:00</lastmod>
  <changefreq>monthly</changefreq>
  <priority>1</priority>
  </url>';
  //Connect to the database, ignored here
  //Query the database. try is not set here. Please add it if the project is used. Query the article table in reverse order
  $sql="select * from writings order by `id` desc";
  $smt=$pdo->prepare($sql);
  $smt->execute();
  $rows=$smt->fetchAll(PDO::FETCH_ASSOC);
  //Determine whether $rows has data
  if(!$rows){echo '<h1 style="color: #7D3C98; "> no data! < / H1 > ';} 
  //Cyclic output data
  foreach($rows as $row){
  if($row['id']==count($rows)){$priority = '0.8';}else{$priority = '0.5';}// To determine whether it is the last field, set $priority to 0.8, otherwise set to 0.5
  $row['time'] = new DateTime($row['time']);
  $row['time'] = $row['time']->format(DateTime::ATOM); // The format time is ISO8601
  echo '<url>
  <!-- Output data, because my article links are in numbers id Is linked, so the writing format is-https://qagzs.com:88 / Joan's article / id.html -- >
  <loc>https://qagzs.com:88 / Joan's article / '$ row['id'].'. html</loc>
  <!-- output ISO8601 Time format for -->
  <lastmod>'.$row['time'].'</lastmod>
  <!-- The output update frequency defaults to monthly That's all -->
  <changefreq>monthly</changefreq>
  <!-- Output pass judgment $row['id']obtain $priority Value of -->
  <priority>'.$priority.'</priority>
  </url>';
  }
  //The loop ends, then disconnects, and outputs a footnote
  $pdo = null;
  echo '</urlset>';

If there are no errors, the output should be the same as the following links https://qagzs.com:88/ sitemap / sitemap
Write it and resubmit it to Google
Original link

Keywords: PHP seo

Added by Cinds on Thu, 27 Jan 2022 23:24:52 +0200