Log in

View Full Version : PHP creating an XML file



GrahamZA
01-12-2011, 08:26 PM
Hi all. My first time here.

I’m very new to PHP, but am having trouble with this project. My task (given by myself) is to pull product information from a product database (OpenCart) and plonk it into an XML file.

What I still need to do with the file that I’m struggling with:
It needs to generate the .xml file in the root directory where the .php file is
It needs to generate the file at 9pm every night

This is my .php file so far


<?php

header('Content-type: text/xml');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="products.xml"');

// connect to database
$db_name = "database";
$connection = mysql_connect("localhost", "user", "password") or die("Could not connect.");

// querying mysql
$db = mysql_select_db($db_name, $connection) or die("Could not connect.");;

$result = mysql_query("SELECT * FROM product_description NATURAL JOIN product");

// XML File Creation
$buf ='<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ROOT>
<Products>
';

while($row=mysql_fetch_array($result)) {

$buf .= "<Product>
<ProductCode><![CDATA[ ".htmlentities($row[7]) ." ]]></ProductCode>
<Title><![CDATA[ ". htmlentities($row[2]) ." ]]></Title>
<Category><![CDATA[ categories ]]></Category>
<Price>". htmlentities($row[14]) ."</Price>
<Quantity>". htmlentities($row[9]) ."</Quantity>
<Condition>New</Condition>
<Location>Cape Town</Location>
<ShippingOption>Speed Services</ShippingOption>
<ImageURL><![ CDATA[ http://www.digitalsushi.co.za/image/". htmlentities($row[11]) ." ]]></ImageURL>
<Description><![CDATA[ ".html_entity_decode($row[5]) ." ]]></Description>
</Product>
";
}

$buf .= "</Products>
</ROOT>";

echo $buf;

mysql_close($connection);;
$buf="";

?>



Can anyone help me with this?

nextdownload
01-13-2011, 02:33 PM
$file=fopen("xmlfile.xml","w");
fwrite($file,$buf);
fclose($file)

this is to create a xml file with your products.

if you need it to run at 9am you need to create an crontab job for you .php script

GrahamZA
01-13-2011, 02:38 PM
Thank you for the reply. Where would I plonk that into the sheet?

I managed to figure out the cronjob with this code, but I still need to test it.

lynx -source http://digitalsushi.co.za/products.php > /dev/null