0

I have the following code which is displaying an xml page as with the URL 'ourcustomersdb.php'. I need this same information to be displayed with the URL ending in 'ourcustomers.xml'. (Hope that makes sense)

The code I have works, but I need the last line of code to create the xml document:

<?php
header ("Content-Type:text/xml");//Tell browser to expect xml
include ("config/init.php");
$query = "SELECT * FROM ourcustomers"; 
$result = mysqli_query($mysqli_conn, $query) or die ("Error in query: $query. ".mysql_error()); 
//Top of xml file
$_xml = '<?xml version="1.0"?>'; 
$_xml .="<ourcustomers>"; 
while($row = mysqli_fetch_array($result)) { 
$_xml .="<ourcustomer>"; 
$_xml .="<id>".$row['id']."</id>"; 
$_xml .="<customer>".$row['customer']."</customer>"; 
$_xml .="<work_done>".$row['work_done']."</work_done>"; 
$_xml .="</ourcustomer>"; 
} 
$_xml .="</ourcustomers>"; 
//Parse and create an xml object using the string
$xmlobj=new SimpleXMLElement($_xml);
//output
//print $xmlobj->asXML();
//write to a file
$xmlobj->asXML(ourcustomers.xml);
?>

The line that doesn't seem to be working is the $xmlobj->asXML(ourcustomers.xml). I need this to take the information gathered from the database to create an XML file. If I leave in the last print comment it displays the XML but on the page ourcustomersdb.php, but I need this to be displayed on ourcustomers.xml (hope this is making sense!). Can someone explain why as this is the first time I'm trying to do this so I'm not fully understanding it.

Any help would be great!

alwaystrying
  • 163
  • 1
  • 1
  • 9
  • Help: Enable error reporting and logging to the highest level on your development machine, fix all errors, warnings, notices and strict warnings first before putting the code into a question. Additionally do not post dead code (e.g. those lines you commented out). For a question is very useful to not post your actual code but to create a new example from scratch that is demonstrating your issue with *as little code as necessary* and containing all data (and as little again as necessary) so it's clear what you ask about and you won't get distracted. – hakre Jan 12 '15 at 10:06

1 Answers1

1

You need to wrap ourcustomers.xml in quotes, so that it is a string. Otherwise you will get a syntax error.

$xmlobj->asXML('ourcustomers.xml');

Also, you don't have to use SimpleXMLElement to write an XML file, you could just use file_put_contents.

file_put_contents('ourcustomers.xml', $_xml);
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171