I was searching for a simple script to generate a dynamic sitemap when i came across what i have below:
<?php
header("Content-Type: application/xml; charset=utf-8");
echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;
echo '<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">' .PHP_EOL;
function urlElement($url) {
echo '<url>'.PHP_EOL;
echo '<loc>'.$url.'</loc>'. PHP_EOL;
echo '<changefreq>weekly</changefreq>'.PHP_EOL;
echo '</url>'.PHP_EOL;
}
urlElement('https://www.example.com/sub1');
urlElement('https://www.example.com/sub2');
urlElement('https://www.example.com/sub2');
echo '</urlset>';
?>
The above code works perfectly to generate a sitemap for the specified urls but i need something that can loop through the specified urls and fetch all the links on them to create a single sitemap while ignoring duplicate links.
Store your URLs in an array then do a foreach() on the array and call urlElement($value); on the value within the loop.
<?php
function urlElement($url) {
echo '<url>'.PHP_EOL;
echo '<loc>'.$url.'</loc>'. PHP_EOL;
echo '<changefreq>weekly</changefreq>'.PHP_EOL;
echo '</url>'.PHP_EOL;
}
$urls = array(
"http://www.google.com",
"http://www.google.com/images",
"http://www.google.com/maps"
);
header("Content-Type: application/xml; charset=utf-8");
echo '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;
echo '<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">' .PHP_EOL;
foreach($urls as $value) {
urlElement($value);
}
echo '</urlset>';
?>
Related
Here is my XML. I need to print with tags.
$XMLData = '<?xml version="1.0" encoding="utf-8"?>
<GetItemRequest xmlns="urn:ebay:apis:eBLBaseComponents">
<RequesterCredentials>
<eBayAuthToken>ABC</eBayAuthToken>
</RequesterCredentials>
<ItemID>123456</ItemID>
<DetailLevel>ReturnAll</DetailLevel>';
$XMLData .= '<IncludeItemSpecifics>True</IncludeItemSpecifics>
</GetItemRequest>';
echo $XMLData;
Thank you.
You can use this for show without html tag :
$XMLData = 'Your Html Code';
echo htmlentities($XMLData);
You can try this:
echo htmlentities($XMLData);
For line break Try:
echo nl2br(htmlentities($XMLData));
Php Documention: http://php.net/manual/en/function.htmlentities.php
I have oai.php page.Now when i type in url oai.php?verb=identify then want to show the contents in identify.xml.But i want the same url(oai.php?verb=identify) when i view the identify.xml.Below code showing only data of xml.
<?php
include "config.php";
$id= $_GET['verb'];
//echo $id;
if($id=="identify")
{
$date_mod = gmdate("Y-m-d\TH:i:s\Z");
$rss_txt ='<?xml version="1.0" encoding="utf-8"?>
<OAI-PMH xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.openarchives.org/OAI/2.0/" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
<responseDate>' .$date_mod. '</responseDate>
<request verb="Identify">http://mywebsite.org/oai</request>
<Identify>
<repositoryName>mydomain</repositoryName>
<baseURL>mywesite.com</baseURL>
<protocolVersion>2.0</protocolVersion>
<adminEmail>myemail</adminEmail>
<earliestDatestamp>'.$date_mod.'</earliestDatestamp>
<deletedRecord>transient</deletedRecord>
<granularity>YYYY-MM-DDThh:mm:ssZ</granularity>
</Identify>
</OAI-PMH>';
}
else if($id=="ListRecords")
{
include "config.php";
$date_mod = gmdate("Y-m-d\TH:i:s\Z");
$viewss1=$con->query("SELECT COUNT(upload_paper_id) FROM upload_papers");
$cview1=$viewss1->fetch_row();
$countt1=$cview1[0];
$rss_txt = '<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="http://localhost/oai-file/data/oai2.xsl" ?>
<OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
<responseDate>' .$date_mod. '</responseDate>
<request verb="ListRecords" metadataPrefix="oai_dc">http:mywebsite.org</request>
<ListRecords>';
$query = "SELECT * FROM upload_papers";
$result=$con->query($query);
while($values_query = mysqli_fetch_assoc($result))
{
$year= $values_query['year'];
$upload_paper_id= $values_query['upload_paper_id'];
$rss_txt = '<record>
<header>
<identifier>' .$upload_paper_id. '</identifier>
<datestamp>' .$date_mod. '</datestamp>
<setSpec>mydoc8:BIH</setSpec>
</header>
<metadata>
<oai_dc:dc
xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/
http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
<dc:title xml:lang="en-US">' .$values_query['paper_title']. '</dc:title>
<dc:creator>' . $values_query['author_name']. '</dc:creator>
<dc:creator>' . $values_query['co_author_name']. '</dc:creator>
<dc:description xml:lang="en-US">' .$values_query['abstract']. '</dc:description>
<dc:publisher xml:lang="en-US">' .$values_query['journal_nam']. '</dc:publisher>
<dc:contributor xml:lang="en-US">' .$values_query['author_name']. '</dc:contributor>
<dc:type xml:lang="en-US">Article</dc:type>
<dc:format>application/pdf</dc:format>
<dc:identifier>http://mywebsite'.$year.'/article.php?page='.$upload_paper_id.'</dc:identifier>
<dc:language>en</dc:language>
</oai_dc:dc>
</metadata>
</record>';
}
$rss_txt = '<resumptionToken
completeListSize="'.$countt1.'"
cursor="0">'.$date_mod.'</resumptionToken>
</ListRecords>
</OAI-PMH>';
}
else if($id=="ListSets")
{
$rss_txt =file_get_contents("ListSets.xml");
}
header('Content-type: application/xml');
echo $rss_txt;
?>
Above code shows only the data of xml.The output attached below
And i want the output like
.If i type verb=identify in url then it want to show identify.xsd or xml file any one.Then i type Listsets Want to show the content in that page.if verb equal to empty want to show the identify page.Please help me.Its really irritating me.Thank u
There are a few problems with the code. The main thing is to set your header to be XML content type. This will tell the browser what to expect. But there are a few other issues.
$id= $_GET['verb'];
//echo $id;
if($id=="identify")
{
$date_mod = gmdate("Y-m-d\TH:i:s\Z");
$rss_txt ='<?xml version="1.0" encoding="utf-8"?>
<OAI-PMH xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.openarchives.org/OAI/2.0/" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
<responseDate>' .$date_mod. '</responseDate>
<request verb="Identify">http://mywebsite.org/oai</request>
<Identify>
<repositoryName>mydomain</repositoryName>
<baseURL>mywesite.com</baseURL>
<protocolVersion>2.0</protocolVersion>
<adminEmail>myemail</adminEmail>
<earliestDatestamp>'.$date_mod.'</earliestDatestamp>
<deletedRecord>transient</deletedRecord>
<granularity>YYYY-MM-DDThh:mm:ssZ</granularity>
</Identify>
</OAI-PMH>';
}
else if($id=="ListRecords")
{
$rss_txt =file_get_contents("compend1.xml");
}
else if($id=="ListSets")
{
$rss_txt =file_get_contents("ListSets.xml");
}
header('Content-type: application/xml');
echo $rss_txt;
You had $rss_txt .= which with the . means append the string, which as you didn't declare it anywhere gives you the error about an undefined variable. Also with the content - XML needs to start right at the beginning of the string, so remove the whitespace.
The main one is having...
header('Content-type: application/xml');
But, for this to work properly, you need to ensure you comment out the echo, which I assume was some debugging code.
When doing an if - a single = is used to assign a value, so you need == to compare the values.
You can use $_GET[] to access the parameters rather than the way you were using, a lot simpler.
I've used file_get_contents() to fetch the data from those other files and moved the echo to the end so they all have the same logic.
This is my code on file sitemap.php for my dynamic sitemap but I want to print or show this whole page data on "sitemap.xml" because of Google always consider sitemap.xml for sitemap.This page is working fine but I want all data on sitemap.xml.
How to do this?
<?php
header("Content-type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8" ?>';
include 'includes/connectdb.php';
?>
<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">
<url>
<loc>http://www.mobilesapp.com/</loc>
<priority>1.00</priority>
</url>
<?php
$stmt = $con->prepare("SELECT url,modified FROM localnews");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row){
?>
<url>
<loc>http://www.mobilesapp.com/<?= $row['url'] ;?></loc>
<lastmod><?= $row['modified']; ?></lastmod>
<changefreq>always</changefreq>
<priority>1</priority>
</url>
<?php } ?>
<?php
$stmt = $con->prepare("SELECT url,modified FROM socialnews");
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rows as $row){
?>
<url>
<loc>http://www.mobilesapp.com/<?= $row['url'] ;?></loc>
<lastmod><?= $row['modified']; ?></lastmod>
<changefreq>always</changefreq>
<priority>1</priority>
</url>
<?php } ?>
</urlset>
<?php
$xml = '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">';
// mysql select
foreach ($rows as $row) {
// add next part to xml
$xml .= '
<url>
<loc>http://www.mobilesapp.com/'.$row['url'].'</loc>
<lastmod>'.$row['modified'].'</lastmod>
<changefreq>always</changefreq>
<priority>1</priority>
</url>
';
}
$xml .= '</urlset>';
// show
echo $xml;
// and save to file
file_put_contents('sitemap.xml', $xml);
?>
I am trying to create sitemap in my codeigniter app following this answer
Here is my controller method:
public function siteMap() {
$this->load->helper('url');
$urls = array("test");
$data['urls'] = $urls;
$data['frontend'] = $this->getFronendItems();
$this->load->template('front/site_map.php', $data);
}
And my view:
<?php header('Content-type: text/xml'); ?>
<?= '<?xml version="1.0" encoding="UTF-8" ?>' ?>
<url>
<loc><?= base_url() ?></loc>
<priority>1.0</priority>
</url>
<?php foreach($urls as $url) { ?>
<url>
<loc><?= base_url() . $url ?></loc>
<priority>0.5</priority>
</url>
<?php } ?>
This raises the following error:
This page contains the following errors:
error on line 41 at column 8: Opening and ending tag mismatch: link line 0 and head
Tried to remove the header and the url is just echoing as a string on the screen. What am I doing wrong ?
You should always place a header above any view files,
usually I place mine at the top of a controller method.
public function siteMap() {
header("Content-Type: text/xml;charset=iso-8859-1");
$this->load->helper('url');
$urls = array("test");
$data['urls'] = $urls;
$data['frontend'] = $this->getFronendItems();
$this->load->template('front/site_map.php', $data);
}
in the view file, add
<?php print '<?xml version="1.0" encoding="utf-8"?>';?>
<?php print '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';?>
<url>
<loc><?=base_url()?></loc>
<priority>1.0</priority>
</url>
<?php foreach($urls as $url) {
?>
<url>
<loc><?=base_url().$url?></loc>
<priority>0.5</priority>
</url>
<?php
}
?>
<?php print '</urlset>';?>
This is exactly how I do my sitemap.
I have written the following code to understand how php can be used to get and write data to xml files:
<?php
if (file_exists('/requests.xml')) {
$xml = simplexml_load_file('requests.xml');
foreach($xml->data->requests->request as $req)
{
print "Loop entered";
print $req->ip;
print $req->timelast;
}
}
?>
The xml file requests.xml follows:
<?xml version="1.0" encoding="utf-8"?>
<data>
<requests>
<request>
<ip>6.6.6.6</ip>
<timelast>2014-05-30 11:38:23</timelast>
</request>
</requests>
</data>
The problem is that when the script is run, it does not display anything in the browser. In fact it does not enter the loop.
I'm definitely missing something basic.
$xml will take your default node auto so no need to fetch result with data try
foreach($xml->requests->request as $req)
also change
if (file_exists('/requests.xml')) {
to
if (file_exists('requests.xml')) { // if same dir
i have tried like:-
$xml ='<?xml version="1.0" encoding="utf-8"?>
<data>
<requests>
<request>
<ip>6.6.6.6</ip>
<timelast>2014-05-30 11:38:23</timelast>
</request>
</requests>
</data>';
$xml = simplexml_load_string($xml);
foreach($xml->requests->request as $req)
{
print "Loop entered";
print $req->ip;
print $req->timelast;
}
output :- Loop entered6.6.6.62014-05-30 11:38:23
<?php
try
{
$feed = new SimpleXMLElement('requests.xml', null, true);
}
catch(Exception $e)
{
echo $e->getMessage();
exit;
}
foreach($feed->member as $property)
{
echo $property->id;
echo $property->lastName;
}
?>
XML :
1
MSDWEr