Create output XML file in PHP
Have a look at:
- SimpleXML
- Introducing SimpleXML in PHP 5
- Read and write XML with PHP
Sitemap Dynamic Creation with PHP
I had a similar issue before but solved it by running a different PHP file to update the sitemap with a CRON job.
<?php
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>';
$xmlString .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
$xmlString .= '<url>';
$xmlString .= '<loc>http://example.com/</loc>';
$xmlString .= '<lastmod>'.date(DATE_ATOM,time()).'</lastmod>';
$xmlString .= '<changefreq>daily</changefreq>';
$xmlString .= '<priority>1.0</priority>';
$xmlString .= '</url>';
$xmlString .= '<url>';
$xmlString .= '<loc>http://example.com/videos/</loc>';
$xmlString .= '<lastmod>'.date(DATE_ATOM,time()).'</lastmod>';
$xmlString .= '<changefreq>daily</changefreq>';
$xmlString .= '<priority>1.0</priority>';
$xmlString .= '</url>';
$xmlString .= '<url>';
$xmlString .= '<loc>http://example.com/contact/</loc>';
$xmlString .= '<lastmod>'.date(DATE_ATOM,time()).'</lastmod>';
$xmlString .= '<changefreq>daily</changefreq>';
$xmlString .= '<priority>1.0</priority>';
$xmlString .= '</url>';
$xmlString .= '<url>';
$xmlString .= '<loc>http://example.com/blog/</loc>';
$xmlString .= '<lastmod>'.date(DATE_ATOM,time()).'</lastmod>';
$xmlString .= '<changefreq>daily</changefreq>';
$xmlString .= '<priority>1.0</priority>';
$xmlString .= '</url>';
$sql = "SELECT * FROM categories";
$stmt = DB::run($sql);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$url = $row["url"];
$xmlString .= '<url>';
$xmlString .= '<loc>http://example.com/category/'.htmlentities($url).'/</loc>';
$xmlString .= '<lastmod>'.date(DATE_ATOM,time()).'</lastmod>';
$xmlString .= '<changefreq>daily</changefreq>';
$xmlString .= '<priority>1.0</priority>';
$xmlString .= '</url>';
}
$xmlString .= '</urlset>';
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xmlString);
$dom->save('../sitemap.xml');
?>
Edit
<?php
$host = 'http://www.google.com/ping?sitemap=https://www.example.com/sitemap.xml';
$ch = curl_init($host);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode>=200 && $httpcode<300){
echo 'success';
} else {
echo 'try again';
}
?>
How to make a dynamic xml file that I can access through js with php
Add an application/xml
header:
<?php header("Content-type: application/xml"); ?>
<?xml version="1.0" encoding="UTF-8"?>
<?php
echo'<user>';
echo '<link>'."Plain Text".'</link>';
echo'</user>';
Output in Firefox:
creating dynamic xml with php
Using DomDocument is one option:
$dom = new DomDocument;
$dom->loadXml($xml);
$videoPaths = $dom->getElementsByTagName('videoPath');
foreach ($videoPaths as $videoPath) {
$videoPath->setAttribute('value', 'newvideo.mp4');
}
$previewImages = $dom->getElementsByTagName('previewImage');
foreach ($previewImages as $previewImage) {
$previewImage->setAttribute('value', 'newimage.jpg');
}
$xml = $dom->saveXml();
SimpleXML may also suit.
Is there any way to put CSS link in XML file dynamically generated by php?
To link an CSS in XML you can use the xml-stylesheet
processing instruction. Use the corresponding method on DOMDocument to create it and then append/insert it just like any other node.
$document = new DOMDocument();
$document->appendChild(
$document->createProcessingInstruction(
'xml-stylesheet', 'type="text/css" href="styles.css"'
)
);
$document->appendChild(
$document->createElement('example')
);
echo $document->saveXML();
Output:
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="styles.css"?>
<example/>
Related Topics
PHP Method Chaining or Fluent Interface
Escaping Quotation Marks in PHP
Check If PHP Session Has Already Started
PHP Function to Generate V4 Uuid
Multiple Returns from a Function
When Do I Use the PHP Constant "PHP_Eol"
How to Get Id of the Last Updated Row in MySQL
Detect Browser Language in PHP
How to Convert an Object to an Array
How to Create a Comma-separated List from an Array in PHP
Split a Comma-Delimited String into an Array
How to Store My Users' Passwords Safely
Convert from MySQL Datetime to Another Format With PHP
Creating Default Object from Empty Value in PHP
Convert Base64 String to an Image File