Jump to content

Using Output buffer


Lassie

Recommended Posts

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();?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.