SimplePie::handle_content_type PHP Method

handle_content_type() public method

This method ensures that the SimplePie-enabled page is being served with the correct {@link http://www.iana.org/assignments/media-types/ mime-type} and character encoding HTTP headers (character encoding determined by the {@see \set_output_encoding} config option). This won't work properly if any content or whitespace has already been sent to the browser, because it relies on PHP's {@link http://php.net/header header()} function, and these are the circumstances under which the function works. Because it's setting these settings for the entire page (as is the nature of HTTP headers), this should only be used once per page (again, at the top).
public handle_content_type ( string $mime = 'text/html' )
$mime string MIME type to serve the page as
    public function handle_content_type($mime = 'text/html')
    {
        if (!headers_sent()) {
            $header = "Content-type: {$mime};";
            if ($this->get_encoding()) {
                $header .= ' charset=' . $this->get_encoding();
            } else {
                $header .= ' charset=UTF-8';
            }
            header($header);
        }
    }

Usage Example

Example #1
0
 /**
  * Display a wildcard in the back end
  *
  * @return string
  */
 public function generate()
 {
     if (TL_MODE == 'BE') {
         /** @var \BackendTemplate|object $objTemplate */
         $objTemplate = new \BackendTemplate('be_wildcard');
         $objTemplate->wildcard = '### ' . utf8_strtoupper($GLOBALS['TL_LANG']['FMD']['rss_reader'][0]) . ' ###';
         $objTemplate->title = $this->headline;
         $objTemplate->id = $this->id;
         $objTemplate->link = $this->name;
         $objTemplate->href = '' . $GLOBALS['TL_CONFIG']['backendPath'] . '/main.php?do=themes&table=tl_module&act=edit&id=' . $this->id;
         return $objTemplate->parse();
     }
     $this->objFeed = new \SimplePie();
     $arrUrls = trimsplit('[\\n\\t ]', trim($this->rss_feed));
     if (count($arrUrls) > 1) {
         $this->objFeed->set_feed_url($arrUrls);
     } else {
         $this->objFeed->set_feed_url($arrUrls[0]);
     }
     $this->objFeed->set_output_encoding(\Config::get('characterSet'));
     $this->objFeed->set_cache_location(TL_ROOT . '/system/tmp');
     $this->objFeed->enable_cache(false);
     if ($this->rss_cache > 0) {
         $this->objFeed->enable_cache(true);
         $this->objFeed->set_cache_duration($this->rss_cache);
     }
     if (!$this->objFeed->init()) {
         $this->log('Error importing RSS feed "' . $this->rss_feed . '"', __METHOD__, TL_ERROR);
         return '';
     }
     $this->objFeed->handle_content_type();
     return parent::generate();
 }
All Usage Examples Of SimplePie::handle_content_type