Question : How to combine XML & PHP

Hello,

I bought XML maps and I'm unsure how to implement php in them.  This is their code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
<county>
 <id>1</id>
 <name>Aleutians East</name>
 <URL>http://www.domain.com</URL>
 <info>Aleutians East currently has 0 listings</info>
 <color1>0x7798BA</color1>
 <color2>0x366CA3</color2>
 <frame></frame>
 <image></image>
</county>


I need to take info out of the database and have it put into the code.  "counties.php" is what does this.  Here's what I tried that failed miserably:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
<?php include ("counties.php");?>
<county>
 <id>1</id>
 <name>Aleutians East</name>
 <URL>http://www.domain.com</URL>
 <info>Aleutians East currently has <?php echo $number; ?> listings</info>
 <color1>0x7798BA</color1>
 <color2>0x366CA3</color2>
 <frame></frame>
 <image></image>
</county>


Obviously this doesn't work with XML.  Any ideas on how to do this?

Thank you in advance!
~Amy

Answer : How to combine XML & PHP

Thanks.  I think the overall strategy is to parse PHP instructions and create XML output.  You can write the XML file right into the file system with file_put_contents() or you can force a download of the XML file to the client's computer or you can echo the XML directly into the browser output stream.  It will depend on what kind of web service you're hoping to provide.

You probably want to add something like this to the top of the XML string:

$xml = '<?xml version="1.0"?>

Here is a PHP function that will force a download.  As you can see, it expects a file name, but you could easily modify it to accept a string for the input.  To do that , you would modify it to have two arguments, the string and a "basename" for the downloaded file.

HTH, ~Ray

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
// FUNCTION TO FORCE A DOWNLOAD
function force_download($filename)
{
    // GET A NAME FOR THE FILE
    $basename = basename($filename);

    // GET THE CONTENTS OF THE FILE
    $filedata = file_get_contents($filename);

    if ($filedata)
    {
        // THESE HEADERS ARE USED ON ALL BROWSERS
        header("Content-Type: application-x/force-download");
        header("Content-Disposition: attachment; filename=\"$basename\"");
        header("Content-length: ".(string)(strlen($filedata)));
        header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
        header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

        // THIS HEADER MUST BE OMITTED FOR IE 6+
        if (FALSE === strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE '))
        {
            header("Cache-Control: no-cache, must-revalidate");
        }

        // THIS IS THE LAST HEADER
        header("Pragma: no-cache");

        // FLUSH THE HEADERS TO THE BROWSER
        flush();

        // CAPTURE THE FILE IN THE OUTPUT BUFFERS - WILL BE FLUSHED AT SCRIPT END
        ob_start();
        echo $filedata;
    }
}
Random Solutions  
 
programming4us programming4us