I've got a script put togheter form other sources, not my own thinking. The reason for me needing to get this to work is to get a drupal top menu into top of an opencart installation to be able to use the superior cms functionalites while letting opencart handle the superior web shop functions.
The problem is that the menu that prints to a cache file from the script uses relative url.s. This of course means that when the menu is required into opencart, which is on a subdomain, the paths show the path of the subdomain. Not good.
EDIT: Ok, I got the code working, got a little confused for a while, what with all $content and $contents everywhere. Changing code below to how it looks now. I needed to change the time interval to 1 second, otherwise the drupal page behaved very oddly.
<?php
$cache_time = 1; // Time in seconds to keep a page cached
$cache_folder = 'cache'; // Folder to store cached files (no trailing slash)
$cache_filename = $cache_folder.md5($_SERVER['REQUEST_URI']);
// Location to lookup or store cached file
//Check to see if this file has already been cached
// If it has get and store the file creation time
$cache_created = (file_exists($cache_file_name)) ? filemtime($cache_filename) : 0;
if ((time() - $cache_created) < $cache_time) {
readfile($cache_filename);
// The cached copy is still valid, read it into the output buffer
die();
}
?>
<?php ob_start(); // Turns on output buffering ?>
<?php $contents = ob_get_contents(); ?>
<!-- //Stores the contents of the buffer in a variable as a string -->
<?php if ($page['header_menu']): ?>
<div id="header-menu-wrapper">
<div id="header-menu">
<?php print render($page['header_menu']); ?>
</div>
</div>
<?php endif; ?>
<?php
$contents = ob_get_contents();
$contents = str_replace('href="','href="http://yourdomain.com', $contents);
file_put_contents($cache_filename, $contents());
ob_end_flush();
?>
Also, it would be great to be able to get the cached file always be written to the same filename, otherwise it wouldn't do much good since the menu in the shop would't change when the drupal menu does since the change would result in a new filename.
I've looked at other questions here regarding drupal and absolute paths but either I don't understand enough or they are not applicable.