How to Generate Xml File Dynamically Using PHP

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:
Sample Image

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



Leave a reply



Submit