Lassie Posted July 22, 2009 Share Posted July 22, 2009 I have a page that I want to output xml rss data. I have used output buffering but still get header already sent error. I tried changing the logic so that page output was at the end but that esulted in no xml. Can anyone advise how to organise this code to avoid the problem. <?php ob_start(); /* Template Name: Feed */ ?> <?php get_header();?> <div id="leftnav"> <?php include (TEMPLATEPATH . '/sidebar1.php'); ?> <div id="right_pan"> </div> </div> <div id="rightnav"> <?php include (TEMPLATEPATH . '/sidebar2.php'); ?> </div> <div id="content"> <p> New Submissions Feed </p> <?php require_once '../Pos/RssFeed.php'; require_once '../Pos/MysqlImprovedConnection.php'; require_once '../Pos/MysqlImprovedResult.php'; try { $xml = new Pos_RssFeed('localhost', 'xxxx', 'xxxx', 'xxxx'); $xml->setFeedTitle('Authorbank New Submissions'); $xml->setFeedLink('http://www.example.com/oop_news.xml'); $xml->setFeedDescription('New Authors and New writing for Publishers.'); $xml->setLastBuildDate(true); // $xml->setFilePath('oop_news.xml'); $xml->setItemTitle('WorkingTitle'); $xml->setItemDescription('BriefDescription'); $xml->setItemPubDate('updated'); $xml->setTable('book'); $xml->setItemLinkURL('http://www.example.com/detail.php'); // $xml->setItemLink('url'); $result = $xml->generateXML(); if ($result) { ob_end_flush(); header('Content-Type: text/xml'); echo $result; } else { echo 'Error'; } } catch (Exception $e) { echo $e->getMessage(); } ?> </div> <?php get_footer();?> Quote Link to comment https://forums.phpfreaks.com/topic/166968-using-output-buffer/ Share on other sites More sharing options...
Adam Posted July 22, 2009 Share Posted July 22, 2009 You're sending a header straight after you flush the buffer: ob_end_flush(); header('Content-Type: text/xml'); Quote Link to comment https://forums.phpfreaks.com/topic/166968-using-output-buffer/#findComment-880290 Share on other sites More sharing options...
haku Posted July 22, 2009 Share Posted July 22, 2009 And this is a bad usage of the output buffer anyways. You should re-structure your code so you don't need it. All it's doing is masking the flaws in your structural logic. Quote Link to comment https://forums.phpfreaks.com/topic/166968-using-output-buffer/#findComment-880312 Share on other sites More sharing options...
Lassie Posted July 22, 2009 Author Share Posted July 22, 2009 Thanks. I have tried restructuring the code but when I do my xml is not generated. The page is a custom wordpress page that has to use a template at the top of the script. If i dont include the the page cant be found it seems. I have also tried just echo out to the browser but again this fails. I know the buffer idea is lazy but i cant see anther way. Would you have any more pointers please. Quote Link to comment https://forums.phpfreaks.com/topic/166968-using-output-buffer/#findComment-880438 Share on other sites More sharing options...
PFMaBiSmAd Posted July 22, 2009 Share Posted July 22, 2009 You cannot output an XML header and XML data on a web page that already has output HTML. A page/document must be one type or the other other. Output buffering cannot solve this problem. If you are trying to output an XML document when you browse to the page that contains the code you posted, you must only output the XML header and the XML data. If you are trying to make a link in the HTML on that page that when clicked takes you to the XML page, you need to create a separate page/url that only outputs the XML header and XML data. Quote Link to comment https://forums.phpfreaks.com/topic/166968-using-output-buffer/#findComment-880448 Share on other sites More sharing options...
Lassie Posted July 22, 2009 Author Share Posted July 22, 2009 Ok thanks I will work on that. Quote Link to comment https://forums.phpfreaks.com/topic/166968-using-output-buffer/#findComment-880466 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.