Change header("Content-type txt/html");

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
MESWEB
Registered User
Posts: 50
Joined: Sat Sep 26, 2015 1:45 pm

Change header("Content-type txt/html");

Post by MESWEB »

I have mod to generate sitemap and I need use this php code:

Code: Select all

header("Content-type: application/xml");
for change header but this is not working I always have header as txt/html. How can I use xml in phpBB?
Last edited by bonelifer on Thu Jun 23, 2016 7:18 pm, edited 1 time in total.
Reason: Moved from 3.1.x Support Forum
User avatar
MarkDHamill
Registered User
Posts: 4223
Joined: Fri Aug 02, 2002 12:36 am
Location: Florence, MA USA
Contact:

Re: Change header("Content-type txt/html");

Post by MarkDHamill »

In my Smartfeed extension I created a template called feed.xml and placed it inside my extension in styles/all/template. I then used the templating system to populate it with information. You can see it here:

https://github.com/MarkDHamill/smartfee ... e/feed.xml

I never sent anything in the HTTP header to tell pass a content type. It didn't seem to matter. Try my solution and see if it works for you.
Need phpBB services or a phpBB consultant? I offer most phpBB services. Getting lost managing phpBB? Buy my book, Mastering phpBB Administration. Kindle and paper versions available.
User avatar
3Di
Former Team Member
Posts: 15746
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Change header("Content-type txt/html");

Post by 3Di »

MESWEB wrote:I have mod to generate sitemap
Are you MODding phpBB3 or phpBB3.1? Extension or what?
Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate
My development's activity º PhpStorm's proud user
Extensions, Scripts, MOD porting, Update/Upgrades
:studio_microphone: Looking for a specific feature or alternative option?
User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 4321
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Change header("Content-type txt/html");

Post by kasimi »

To modify headers you need to hook into core.page_header_after and overwrite the value for the content type:

Code: Select all

public function page_header_after($event)
{
    $event['http_headers'] = array_merge($event['http_headers'], array(
        'Content-type' => 'application/xml',
    ));
} 
The event is triggered near the end of the function page_header() in /includes/functions.php
MESWEB
Registered User
Posts: 50
Joined: Sat Sep 26, 2015 1:45 pm

Re: Change header("Content-type txt/html");

Post by MESWEB »

kasimi wrote:To modify headers you need to hook into core.page_header_after and overwrite the value for the content type:

Code: Select all

public function page_header_after($event)
{
    $event['http_headers'] = array_merge($event['http_headers'], array(
        'Content-type' => 'application/xml',
    ));
} 
The event is triggered near the end of the function page_header() in /includes/functions.php
I don't know where I should put this to this code:

Code: Select all

class sitemap
{
	/**
	* Constructor
	* NOTE: The parameters of this method must match in order and type with
	* the dependencies defined in the services.yml file for this service.
	*

	/** @var \shredder\sitemap\core */
	protected $core;

	/** @var \phpbb\cache\service */
	protected $cache;

	/** @var \phpbb\config\config */
	protected $config;

	public function __construct(\shredder\sitemap\core $core, \phpbb\cache\service $cache, \phpbb\config\config $config)
	{
		$this->core = $core;
		$this->cache = $cache;
		$this->config = $config;
	}

	/**
	* Controller for route /sitemap/
	*
	* @param string		$name
	* @return \Symfony\Component\HttpFoundation\Response A Symfony Response object
	*/
	public function display_sitemap()
	{
		global $f_xml, $urls;

        $f_xml = '';
		$urls = 0;

		$driver = $this->cache->get_driver();
        //print_r($driver);

		if (($f_xml = $driver->get("_sitemap_seo_file")) === false)
		{
			$this->core->generate_sitemap();

			$this->config->set('sitemap_seo_url_count', $urls);

			if ($this->config['sitemap_seo_cache'])
			{
				$driver->put("_sitemap_seo_file", $f_xml, 3600*$this->config['sitemap_seo_cache']);
			}
		}
		
		return new Response($f_xml);
	}
}
User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 4321
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Change header("Content-type txt/html");

Post by kasimi »

You need to write an event listener class: https://area51.phpbb.com/docs/dev/31x/e ... e-listener
MESWEB
Registered User
Posts: 50
Joined: Sat Sep 26, 2015 1:45 pm

Re: Change header("Content-type txt/html");

Post by MESWEB »

I have listener already working but when I add Your function:

Code: Select all

public function page_header_after($event)
{
    $event['http_headers'] = array_merge($event['http_headers'], array(
        'Content-type' => 'application/xml',
    ));
} 
Nothing happen.
User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 4321
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Change header("Content-type txt/html");

Post by kasimi »

Did you actually hook into to the event?

Code: Select all

public static function getSubscribedEvents()
{
    return array(
        'core.page_header_after' => 'page_header_after',
    );
} 
MESWEB
Registered User
Posts: 50
Joined: Sat Sep 26, 2015 1:45 pm

Re: Change header("Content-type txt/html");

Post by MESWEB »

Yes I have added but nothing change. There is no $event['http_headers']

Code: Select all

<?php

/**
*
* @package phpBB3 SEO Sitemap
* @copyright (c) 2014 www.phpbb-work.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

namespace shredder\sitemap\event;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Event listener
*/
class listener implements EventSubscriberInterface
{
    /** @var \phpbb\auth\auth */
    protected $auth;

    /** @var \phpbb\config\config */
    protected $config;

    /** @var \phpbb\user */
    protected $user;

    /** @var \phpbb\db\driver\driver_interface */
    protected $db;

    /** @var string */
    protected $phpbb_root_path;
    protected $php_ext;

    /**
    * Constructor
    * 
    * @param \phpbb\auth\auth $auth
    * @param \phpbb\config\config $config
    * @param \phpbb\user $user
    * @param \phpbb\db\driver\driver $db
    * @param string $phpbb_root_path Root path
    * @param string $phpbb_ext
    */
    public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\user $user, \phpbb\db\driver\driver_interface $db, $phpbb_root_path, $php_ext)
    {
        $this->auth = $auth;
        $this->config = $config;
        $this->user = $user;
        $this->db = $db;
        $this->phpbb_root_path = $phpbb_root_path;
        $this->php_ext = $php_ext;
    }

    /**
    * Assign functions defined in this class to event listeners in the core
    *
    * @return array
    * @static
    * @access public
    */
    static public function getSubscribedEvents()
    {
        return array(
            'core.user_setup'                    =>    'load_language_on_setup',
            'core.submit_post_end'                    =>    'update_post_modified_time',
            'core.page_header_after' => 'page_header_after',
        );
    }

    public function page_header_after($event)
    {
        $event['http_headers'] = array_merge($event['http_headers'], array(
            'Content-type' => 'application/xml',
        ));
        print_r($event['http_headers']);
    }

    /**
    * Load common files during user setup
    *
    * @param object $event The event object
    * @return null
    * @access public
    */
    public function load_language_on_setup($event)
    {
        $lang_set_ext = $event['lang_set_ext'];
        $lang_set_ext[] = array(
            'ext_name' => 'shredder/sitemap',
            'lang_set' => 'info_acp_seo_sitemap',
        );
        $event['lang_set_ext'] = $lang_set_ext;
    }

    /**
    * Update post modification time when posting or edit
    *
    * @return null
    * @access public
    */
    public function update_post_modified_time($event)
    {
        $data = $event['data'];

        $sql = 'UPDATE ' . POSTS_TABLE . '
                SET post_modified = ' . (int) time() . '
            WHERE post_id = ' . (int) $data['post_id'];
        $this->db->sql_query($sql);
    }
} 
User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 4321
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Change header("Content-type txt/html");

Post by kasimi »

What do you mean there is no event['http_headers']? Is it empty? Are you calling the page_header() function in your extension with the $send_headers argument set to false? In that case the $event['http_headers'] array is empty, but it should work regardless. If $event['http_headers'] doesn't exist at all your includes/functions.php might be outdated: https://github.com/phpbb/phpbb/blob/rel ... .php#L5307

This is what I see when I do print_r($event['http_headers']); in the event handler:

Code: Select all

Array
(
    [Content-type] => text/html; charset=UTF-8
    [Cache-Control] => private, no-cache="set-cookie"
    [Expires] => Sat, 16 Jul 2016 17:56:13 GMT
) 
MESWEB
Registered User
Posts: 50
Joined: Sat Sep 26, 2015 1:45 pm

Re: Change header("Content-type txt/html");

Post by MESWEB »

Yes $event is empty. I have PHPBB 3.1.8 and I have http_headers in function.php :

Code: Select all

    $vars = array('page_title', 'display_online_list', 'item_id', 'item', 'http_headers');
    extract($phpbb_dispatcher->trigger_event('core.page_header_after', compact($vars)));

    foreach ($http_headers as $hname => $hval)
    {
        header((string) $hname . ': ' . (string) $hval);
    }

    return; 
Ok I see now. This is working only for all sites of PHPBB not for file generated by mod. This mod creates file and read content to browser so I need change headers for before save file or display file. So how do this? File is always txt/html
Post Reply

Return to “Extension Writers Discussion”