SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard bronze trophy bigalreturns's Avatar
    Join Date
    Mar 2006
    Location
    The Wirral, England
    Posts
    1,294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    xml generation with php

    I'm trying to make an xml page that will show the 5 most recent submissions, dynamically generated each time the page is loaded. The problem is getting my server to parse the PHP code, but then treat the outcome as xml. I've set apache to handle the .xml extension as php, but this then means that output is given as plain html. Any ideas how I could get round this? Code is shown below.
    Thanks,
    Alex

    Code:
    <?
    include ("dbconnect.inc");
    include ("functions.inc");
    
    $query="SELECT title,author,books.bid,listdate FROM books,booksellerref WHERE books.bid=booksellerref.bid AND booksellerref.dispcheck='y' ORDER BY listdate LIMIT 5";
    $result=mysql_query($query);
    	WHILE ($row=mysql_fetch_assoc($result)) {
    		FOREACH ($row AS $key=>$value) {
    		echo ("<item><title>UniTexts Update</title>");
    			IF ($key=="title") {
    			echo ("<description>" . $value . "</description>");
    			}
    		echo ("</item>");
    		}
    	}
    ?>
    "The proper function of man is to live - not to exist."
    Get a Free TomTom


  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is getting my server to parse the PHP code
    Servers don't parse php code, they hand certain files, e.g. files with a .php extension, to the php engine to parse. When the php engine is done executing the php code, it hands the file back to the server, and the server sends the file to the browser as the response.

    [The problem is getting my server to] treat the outcome as xml
    As far as I know, the server could care less what the outcome is.

  3. #3
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    header("Content-type: text"); 
    That's what you need to insert before any output in php script file. After all, xml file is nothing but a simple text file.

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bigalreturns
    I'm trying to make an xml page that will show the 5 most recent submissions, dynamically generated each time the page is loaded. The problem is getting my server to parse the PHP code, but then treat the outcome as xml. I've set apache to handle the .xml extension as php, but this then means that output is given as plain html. Any ideas how I could get round this? Code is shown below.
    Thanks,
    Alex

    Code:
    <?
    include ("dbconnect.inc");
    include ("functions.inc");
    
    $query="SELECT title,author,books.bid,listdate FROM books,booksellerref WHERE books.bid=booksellerref.bid AND booksellerref.dispcheck='y' ORDER BY listdate LIMIT 5";
    $result=mysql_query($query);
    	WHILE ($row=mysql_fetch_assoc($result)) {
    		FOREACH ($row AS $key=>$value) {
    		echo ("<item><title>UniTexts Update</title>");
    			IF ($key=="title") {
    			echo ("<description>" . $value . "</description>");
    			}
    		echo ("</item>");
    		}
    	}
    ?>
    do the examples onthis thread help at all?

    my extensions are all php - i then use import the xml data using the dom and then use php functions to manipulate the data. or write my own data.

    i wouldn't try this with anythng less than the latest version or two of php - meaning php 5.x.

    if you have php 4, you would probably do better to go out and get a xml php class. phpclasses.org w/b a good place to start.

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    header("Content-type: text");

    That's what you need to insert before any output in php script file.
    Why?

  6. #6
    SitePoint Addict evertcollab's Avatar
    Join Date
    Apr 2006
    Posts
    241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is for the OP:

    You idd have to setup apache to handle .xml files as php..

    you can do this in the .htaccess file or in the main configuration

    AddType application/x-httpd-php .xml

    However!
    This is not best practice. It's better to just keep the .php extension for your php files, even if they output xml. Whats the reason you need the different extension?

    The only possible answer is that it looks nice. If you want to keep things portable and be able to serve 'regular' xml files, don't do this.. (php will throw an error or normal xml files because it sees the <? in <?xml version="1.0"?> as a php opening tag)

    If your still not convinced. Do it with mod_rewrite and map that specific url to your php script.

  7. #7
    SitePoint Zealot
    Join Date
    Apr 2005
    Location
    Helsingborg, Sweden
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by evertcollab
    php will throw an error or normal xml files because it sees the <? in <?xml version="1.0"?> as a php opening tag
    This won't happen if you have disabled shorttags in php.ini. But then you have to make sure all you php start tags are <?php instead of <? which they should be anyway.

  8. #8
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bigalreturns
    I'm trying to make an xml page that will show the 5 most recent submissions, dynamically generated each time the page is loaded. The problem is getting my server to parse the PHP code, but then treat the outcome as xml. I've set apache to handle the .xml extension as php, but this then means that output is given as plain html. Any ideas how I could get round this?
    Try adding
    PHP Code:
    header("Content-type: text/xml");
    echo '<?xml version="1.0" encoding="iso-8859-1"?>';
    before any output.

  9. #9
    SitePoint Wizard bronze trophy bigalreturns's Avatar
    Join Date
    Mar 2006
    Location
    The Wirral, England
    Posts
    1,294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your suggestions, I've now got it working by simply having it as a .php file, adding in the header as suggested...

    header("Content-type: text/xml");
    Seems to have done the trick without having to play around with apache config or anything like tha

    Thanks,
    Alex
    "The proper function of man is to live - not to exist."
    Get a Free TomTom



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •