{"id":18555471,"url":"https://github.com/dialeleven/php-google-sitemap","last_synced_at":"2025-05-15T13:09:52.374Z","repository":{"id":234772966,"uuid":"768451622","full_name":"dialeleven/php-google-sitemap","owner":"dialeleven","description":"PHP Class to generate a Google Sitemap in various formats (XML - list of URLs, images, news, and video sitemaps); it originally only supported XML. Rewritten from older PHP v5.6 code to v8.2 and retooled to use PHP's XMLWriter extension with about 100 less lines of code. On the dev side, it uses PHPUnit for unit testing.","archived":false,"fork":false,"pushed_at":"2024-05-07T13:11:48.000Z","size":1355,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-17T11:26:27.924Z","etag":null,"topics":["google-image-sitemap","google-news-sitemap","google-sitemap","google-sitemap-generator","google-video-sitemap","google-xml-sitemap","html-sitemap","sitemaps","xml","xml-sitemap"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dialeleven.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-03-07T05:28:18.000Z","updated_at":"2024-05-07T13:11:51.000Z","dependencies_parsed_at":"2024-04-20T18:50:16.731Z","dependency_job_id":"f9b416b0-c2ad-4f9d-8693-a11f1ecc1809","html_url":"https://github.com/dialeleven/php-google-sitemap","commit_stats":null,"previous_names":["dialeleven/php-google-sitemap"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dialeleven%2Fphp-google-sitemap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dialeleven%2Fphp-google-sitemap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dialeleven%2Fphp-google-sitemap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dialeleven%2Fphp-google-sitemap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dialeleven","download_url":"https://codeload.github.com/dialeleven/php-google-sitemap/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254346622,"owners_count":22055809,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["google-image-sitemap","google-news-sitemap","google-sitemap","google-sitemap-generator","google-video-sitemap","google-xml-sitemap","html-sitemap","sitemaps","xml","xml-sitemap"],"created_at":"2024-11-06T21:26:49.104Z","updated_at":"2025-05-15T13:09:52.307Z","avatar_url":"https://github.com/dialeleven.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP Google Sitemap - Overview\n\nA PHP class to generate a multiple types of sitemaps. This supports creating a [Google XML Sitemap](https://developers.google.com/search/docs/crawling-indexing/sitemaps/build-sitemap), [image sitemap](https://developers.google.com/search/docs/crawling-indexing/sitemaps/image-sitemaps), [news sitemap](https://developers.google.com/search/docs/crawling-indexing/sitemaps/news-sitemap), and [video sitemap](https://developers.google.com/search/docs/crawling-indexing/sitemaps/video-sitemaps). Most likely you're after creating an XML sitemap, so we'll go over that here.\n\nBriefly, a Google Sitemap (XML/image/news/video) contains two parts:\n\n1. A [Sitemap Index](https://developers.google.com/search/docs/crawling-indexing/sitemaps/large-sitemaps) XML file - a table of contents listing each **urlset** file (this example is in the next bullet point). In the sample sitemap index XML file below, one **urlset** file is named **sitemap1.xml.gz**. Note that we're gzipping the resulting XML file in the example below to reduce file sizes. The XML file can be left uncompressed, but will get rather large with 50,000 URLs in one file which is the maximum per sitemap file (~20MB uncompressed for an XML sitemap). For example:\n\n```\n   \u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n   \u003csitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\u003e\n      \u003csitemap\u003e\n         \u003cloc\u003ehttp://www.mydomain.com/someurl/sitemap1.xml.gz\u003c/loc\u003e\n         \u003clastmod\u003e2024-04-06T21:23:02+00:00\u003c/lastmod\u003e\n      \u003c/sitemap\u003e\n      \u003csitemap\u003e\n         \u003cloc\u003ehttp://www.mydomain.com/someurl/sitemap2.xml.gz\u003c/loc\u003e\n         \u003clastmod\u003e2024-04-06T21:23:02+00:00\u003c/lastmod\u003e\n      \u003c/sitemap\u003e\n   \u003c/sitemapindex\u003e\n```\n\n2. A **urlset** XML file(s) - a list of each of your website's URLs. For example sitemap1.xml may contain something like this for an XML sitemap listing your website's pages/URLs:\n\n```\n   \u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n   \u003curlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\u003e\n      \u003curl\u003e\n         \u003cloc\u003ehttp://www.mydomain.com/someurl/\u003c/loc\u003e\n         \u003clastmod\u003e2024-04-06\u003c/lastmod\u003e\n         \u003cchangefreq\u003eweekly\u003c/changefreq\u003e\n         \u003cpriority\u003e1.0\u003c/priority\u003e\n      \u003c/url\u003e\n      \u003curl\u003e\n         \u003cloc\u003ehttp://www.mydomain.com/anotherurl/\u003c/loc\u003e\n         \u003clastmod\u003e2024-04-07\u003c/lastmod\u003e\n         \u003cchangefreq\u003eweekly\u003c/changefreq\u003e\n         \u003cpriority\u003e1.0\u003c/priority\u003e\n      \u003c/url\u003e\n   \u003c/urlset\u003e\n```\n\nAs you can see the structure is quite similar with the differences being the 'sitemapindex' vs 'urlset' as our opening tag (attributes are identical). The tags contained in our sitemapindex/urlset will contain either a 'sitemap' container tag or 'url' container tag.\n\n## Prerequisites\n\nThere should be no prerequisites needed. This project uses [composer](https://getcomposer.org/) to autoload class files, but the class files have been manually **include**d to avoid requiring the use of composer to simplify using the class for everyone. The /src and /public folders were copied to a separate Apache vhost that doesn't have composer.json in the root or any of the related composer files/dirs (e.g. /vendor) and they were able to generate sitemaps for xml/image/news/video properly using the sample scripts (e.g. /public/1google_xml_sitemap_test.php).\n\n\n## How to use the PHP Google XML Sitemap Class\n\n\u003e [!IMPORTANT]\n\u003e Files you'll need:\n\u003e * /src/AbstractGoogleSitemap.php\n\u003e * /src/GoogleImageSitemap.php\n\u003e * /src/GoogleNewsSitemap.php\n\u003e * /src/GoogleVideoSitemap.php\n\u003e * /src/GoogleXmlSitemap.php\n\n### Sample Usage\nStart off with the required namespace (e.g. \"use _____;\") and include the appropriate class src for the sitemap type you are using. For an XML sitemap, use the GoogleXmlSitemap.php PHP class as shown below.\n```\n   use Dialeleven\\PhpGoogleSitemap;\n\n   // adjust the path to the PHP class depending on your site architecture\n   include_once $_SERVER['DOCUMENT_ROOT'] . '/src/GoogleXmlSitemap.php';\n```\nFor a news sitemap you'll change the *include_once* src to use '/src/GoogleNewsSitemap.php' instead. For example:\n```\n   use Dialeleven\\PhpGoogleSitemap;\n\n   // adjust the path to the PHP class depending on your site architecture\n   include_once $_SERVER['DOCUMENT_ROOT'] . '/src/GoogleNewsSitemap.php';\n```\n\n\n**Create new instance of GoogleSitemap Class**\n\nBy default, resulting XML files will be created in the same path as your script using the PHP Google XML Sitemap class if $xml_files_dir is blank or not passed as an argument.\n\n```\n   // create new instance of the PHP Google XML Sitemap class (using default save dir - whatever script your path is in)\n   /*\n   SPECIFY YOUR SITEMAP TYPE:\n      - xml (for most people, you'll use this unless you need a specialty sitemap type like images, etc..)\n      - image\n      - video\n      - news\n   */\n   $my_sitemap = new Dialeleven\\PhpGoogleSitemap\\GoogleXmlSitemap($sitemap_type = 'xml', $http_hostname = $_SERVER['HTTP_HOST'], $xml_files_dir = '');\n```\n\n**OR**\n\nTo save the resulting XML files saved in a subdirectory, pass the full DOCUMENT_ROOT and directory path(s)\n\n```\n   // create new instance of the PHP Google Sitemap class (using specified XML save directory)\n   $my_sitemap = new Dialeleven\\PhpGoogleSitemap\\GoogleXmlSitemap($sitemap_type = 'xml', $http_hostname = $_SERVER['HTTP_HOST'], $xml_files_dir = $_SERVER['DOCUMENT_ROOT'] . '/public/sitemaps');\n\n```\n\nRemaining logic for usage (please adjust the sample code depending on if you're retrieving the URLs from a database or you have it stored as an array):\n```\n   // Some configuration methods for your sitemap file(s) to be generated.\n   $my_sitemap-\u003esetUseHttpsUrls(true); // use \"https\" scheme (true) for your URLs or plain \"http\" (false)\n   $my_sitemap-\u003esetSitemapFilenamePrefix('mysitemap'); // set name of sitemap file(s) minus \".xml\" (e.g. mysitemap.xml)\n   $my_sitemap-\u003esetUseGzip($use_gzip = false); // gzip the urlset files to reduce file sizes (true/false). NOTE: using gzip will unlink() (i.e. delete) the original XML file(s) after.\n\n   // you might store your url arrays like this\n   $url_md_arr = array(\n      array('http://www.domain.com/url1/', '2024-01-01', 'weekly', '0.5'),\n      array('http://www.domain.com/url2/', '2024-01-01', 'weekly', '0.5'),\n      array('http://www.domain.com/url3/', '2024-01-01', 'weekly', '0.5')\n   );\n\n   // you probably want to pull your URLs from your database though (e.g. MySQL, Postgres, Mongo, etc...)\n   /*\n   INCLUDE YOUR DATABASE LOGIC HERE TO PULL YOUR URLs FROM THE REQUIRED TABLE(s)...\n   */\n\n\n   // loop through your URLs from your array or preferred database (array example for simplicity)\n   foreach ($url_md_arr as $url_arr)\n   {\n      // the important part - adding each URL (replace sample values from your DB/array)\n      $my_sitemap-\u003eaddUrl( $loc = $url_arr[0], $tags_arr = array('lastmod' =\u003e $url_arr[1], 'changefreq' =\u003e $url_arr[2], 'priority' =\u003e $url_arr[3]) );\n   }\n\n\n   // signal when done adding URLs, so we can generate the sitemap index file (table of contents)\n   $my_sitemap-\u003eendXmlDoc();\n```\n**That's all there is to it!**\n\n## About addURL() Method\n\u003e [!NOTE]\n\u003e **For XML sitemap types ONLY**, the **addURL()** method only requires **$loc** to be passed as an argument. \n\u003e The other tags in the $tags_arr for lastmod, changefreq, and priority are optional and can be omitted.\n\u003e \n\nInstead of calling the method like:\n```\n$my_sitemap-\u003eaddUrl( $loc = $url_arr[0], $tags_arr = array('lastmod' = $url_arr[1], 'changefreq' = $url_arr[2], 'priority' = $url_arr[3]) );\n```\n\nYou can just use the following if you don't need lastmod/changefreq/priority:\n```\n$my_sitemap-\u003eaddUrl($loc = $url_arr[0]);\n```\n\n## XML Tag Definitions for XML Sitemaps (e.g. lastmod)\n\n\u003e [!NOTE]\n\u003e If you choose to pass other arguments to addURL() like **lastmod**, **changefreq**, or **priority**, please refer to the following for valid values.\n\nTaken from https://www.sitemaps.org/protocol.html#xmlTagDefinitions\n\n```\u003clastmod\u003e``` optional.\nThe date of last modification of the page. This **date should be in W3C Datetime format**. This format allows you to omit the time portion, if desired, and use **YYYY-MM-DD**.\n\nNote that the date must be set to the date the linked page was last modified, not when the sitemap is generated.\n\nNote also that this tag is separate from the If-Modified-Since (304) header the server can return, and search engines may use the information from both sources differently.\n\n```\u003cchangefreq\u003e``` optional.\nHow frequently the page is likely to change. This value provides general information to search engines and may not correlate exactly to how often they crawl the page. Valid values are:\n\n* always\n* hourly\n* daily\n* weekly\n* monthly\n* yearly\n* never\n\n```\u003cpriority\u003e``` optional.\nThe priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0. This value does not affect how your pages are compared to pages on other sites—it only lets the search engines know which pages you deem most important for the crawlers.\n\nThe default priority of a page is 0.5.\n\n\n## Summary\n\nAs you can see, the usage is pretty simple. \n\n1. Instantiate the class.\n2. A couple configuration methods.\n3. Set up your loop and iterate through your array or database records.\n4. Call addUrl() method until you're out of URLs to add.\n5. Wrap up by calling endXmlDoc() which will generate your sitemapindex TOC.\n6. Submit your sitemapindex XML file to Google. Done!\n\nThis was rewritten from PHP 5.6 to 8 and greatly simplified from a class that\ndid too much and was rather confusing to read and maintain even though it worked.\nIt cut down the lines of code by about 200-300. Hope you find this class useful.\n\n\n## Sample Code for Image/News/Video Sitemaps\n\nThe addURL() method is what changes from one sitemap type to another and will be the focus on the code examples below.\n\n\n### 1. Image Sitemaps\n\nAn [image sitemap](https://developers.google.com/search/docs/crawling-indexing/sitemaps/image-sitemaps) is similar to the XML sitemap addUrl() method. The differences are:\n\n- $tags_arr and $special_tags_arr are not used in the image sitemap (they can be omitted completely, but listed for reference)\n- After calling **addURL()**, you will call **addImage()** N times.\n\n```\n// adjust this loop and logic depending if your URL/image loc's are from a database/text file/array/etc\nfor ($i = 0; $i \u003c= $however_many_urls_you_have; ++$i)\n{\n   // addURL() example for an Image sitemap\n   $my_sitemap-\u003eaddUrl(\n                           $loc = \"url-$i/\",\n                           $tags_arr = array(),\n                           $special_tags_arr = array()\n                        );\n\n   // Output image tag(s) - keep calling addImage() method as many times as needed (one or more times).\n   // Add loop logic as needed.\n   $my_sitemap-\u003eaddImage($loc = \"http://example.com/images/image1.jpg\");\n   $my_sitemap-\u003eaddImage($loc = \"http://example.com/images/image2.jpg\");\n   $my_sitemap-\u003eaddImage($loc = \"http://example.com/images/image3.jpg\");\n}\n```\n\n\n### 2. News Sitemaps\n\nA [news sitemap](https://developers.google.com/search/docs/crawling-indexing/sitemaps/news-sitemap) is the most similar to an XML sitemap with a news sitemap having different **$tags_arr** key/values being passed. All values listed in the $tags_arr (name, language, publication_date, and title) are required for a news sitemap.\n\n```\n// add news sitemap url\n$my_sitemap-\u003eaddUrl(\n                        $loc = \"example-article-title/\",\n                        $tags_arr = array(\n                                             // name/language/publication_date/title are required\n                                             'name' =\u003e \"The Example Times\", \n                                             'language' =\u003e 'en', \n                                             'publication_date' =\u003e '2024-04-19',\n                                             'title' =\u003e \"Example Article Title\"\n                                          )\n                  );\n```\n\n\n### 3. Video Sitemaps\n\nA [video sitemap](https://developers.google.com/search/docs/crawling-indexing/sitemaps/video-sitemaps) has some required and optional tags in the **$tags_arr** as well as some optional tags in the **$special_tags_arr** (these don't conform to the usual $key =\u003e $value that $tags_arr accepts since there are four (4) tag names/values to be passed).\n\n```\n// add video sitemap URL\n$my_sitemap-\u003eaddUrl(\n                        $loc = \"url-$i/\",\n                        $tags_arr = array(\n                                             // these 5 tags are required\n                                             'thumbnail_loc' =\u003e \"https://example.com/thumbs/$i.jpg\", \n                                             'title' =\u003e \"Video Title #$i\", \n                                             'description' =\u003e \"Video description #$i\",\n                                             'content_loc' =\u003e \"http://streamserver.example.com/video$1.mp4\",\n                                             'player_loc' =\u003e \"https://example.com/videoplayer.php?video=$i\"\n\n                                             // optional tags\n                                             /*\n                                             'duration' =\u003e '600', \n                                             'rating' =\u003e '4.2', \n                                             'view_count' =\u003e '12345', \n                                             'publication_date' =\u003e '2007-11-05T19:20:30+08:00', \n                                             'family_friendly' =\u003e 'yes',\n                                             'requires_subscription' =\u003e 'no', \n                                             'live' =\u003e 'no'\n                                             */\n                                          ),\n                        // optional special tags\n                        $special_tags_arr = array(\n                                                   array('restriction', 'relationship', 'allow', 'IE GB US CA'),\n                                                   array('price', 'currency', 'EUR', '1.99'), \n                                                   array('uploader', 'info', \"https://example.com/users/user$i\", \"Username$i\")\n                                                   )\n                     );\n```\n\n\n## Sample Scripts\n\nTL;DR too much reading above? Just want a sample script to get going quickly? Pick your sitemap type (XML/image/video/news) below and customize the sample script to your needs.\n\n- [1google_image_sitemap_test.php](https://github.com/dialeleven/php-google-sitemap/blob/main/public/1google_image_sitemap_test.php)\n- [1google_news_sitemap_test.php](https://github.com/dialeleven/php-google-sitemap/blob/main/public/1google_news_sitemap_test.php)\n- [1google_video_sitemap_test.php](https://github.com/dialeleven/php-google-sitemap/blob/main/public/1google_video_sitemap_test.php)\n- [1google_xml_sitemap_test.php](https://github.com/dialeleven/php-google-sitemap/blob/main/public/1google_xml_sitemap_test.php)\n\n## Conclusion\n\nIf you've gotten this far, I hope you find the class useful! If you have any questions about using the class (XML/image/news/video), bug reports, or constructive feedback, please feel free to reach out!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdialeleven%2Fphp-google-sitemap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdialeleven%2Fphp-google-sitemap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdialeleven%2Fphp-google-sitemap/lists"}