Page 1 of 1

caching php page works but HTML & javascript not rendering

Posted: Tue Jan 29, 2008 1:58 pm
by contractor
Ebay offers ability to include current listings on one's own webpage by using the following JAVASCRIPT link:

Code: Select all

<script type="text/javascript" src="http://lapi.ebay.com/ws/eBayISAPI.dll?EKServer&ai=fjwvl%7Cks&bdrcolor=ff0033&bin=y&cid=0&eksize=1&encode=ISO-8859-1&endcolor=FF0000&endtime=y&fbgcolor=FFFFFF&fntcolor=000000&fs=0&gallery=y&hdrcolor=FFFFCC&hdrimage=1&hdrsrch=n&img=y&lnkcolor=0000FF&logo=1&num=25&numbid=y&paypal=n&popup=y&prvd=1&query=%22microsoft%22&r0=4&shipcost=y&sid=ms&siteid=0&sort=MetaEndSort&sortby=endtime&sortdir=asc&srchdesc=y&tbgcolor=FFFFFF&title=&tlecolor=FFFF99&tlefs=0&tlfcolor=000000&track=2277761&watchcat=39721&width=550"></script>
 
...and this works well if placed in the BODY section of a non-php page. However, in a high traffic/limited resources environment my website's overall response time could be negatively affected if too many web visitors are simultaneously invoking this ebay script.

Since I do, however, want to continue to use this script link and to show relatively fresh content from the ebay site, I wanted to come up with a method that would reduce this ebay content's refresh rate from "every time for each web visitor" to only "once every 30 seconds for all visitors combined".

Thanks to http://www.dangrossman.info/2007/01/03/simple-php-caching-example/ I was able to create a PHP page that performs simple PHP page caching well, but I have not been able to successfully integrate my HTML code.

Refer to the code sample below (you can see the comments towards the middle of the code where dan grossman indicated that my HTML should be placed). Unfortunately this code only renders my HTML as plain text and thus does not invoke the JAVASCRIPT ebay link.

Code: Select all

 
<?php header('Content-Type: application/x-javascript'); 
// check if the cache file already exists
// and if it is less than 60 secs old
$cachefile = './cache/cached.htm';      // note that cached directory must have write permissions
$expiresecs = 60;                       // no. seconds before expire
 
if (file_exists($cachefile) && time() - filemtime($cachefile) < $expiresecs) {
    // the output is already cached, so just dump the file’s contents to the browser
    readfile($cachefile);
} else {
    // otherwise, the cache doesn’t exist yet or has expired, so it’s time to re-generate the content
    // turn on output buffering so your code’s output gets stored in the buffer instead of sent to the browser
    ob_start();
 
    // below here is where you can place the existing PHP and HTML you want to cache
    //-------------------------------------------------------------------------
    ?>
    
    <html><head><title>This page should be cached every 20 seconds</title></head>
    <body>
    <script type="text/javascript" src='http://lapi.ebay.com/ws/eBayISAPI.dll?EKServer&ai=fjwvl%7Cks&bdrcolor=ff0033&bin=y&cid=0&eksize=1&encode=ISO-8859-1&endcolor=FF0000&endtime=y&fbgcolor=FFFFFF&fntcolor=000000&fs=0&gallery=y&hdrcolor=FFFFCC&hdrimage=1&hdrsrch=n&img=y&lnkcolor=0000FF&logo=1&num=25&numbid=y&paypal=n&popup=y&prvd=1&query=%22microsoft%22&r0=4&shipcost=y&sid=ms&siteid=0&sort=MetaEndSort&sortby=endtime&sortdir=asc&srchdesc=y&tbgcolor=FFFFFF&title=microsoft&tlecolor=FFFF99&tlefs=0&tlfcolor=000000&track=2277761&watchcat=39721&width=550'></script>
    <hr />
    </body>
    </html>
    <?php
 
    //-------------------------------------------------------------------------
    // now, write the output you captured in the buffer into the cache file
    $fp = fopen("$cachefile", "w");
    fwrite($fp, ob_get_contents());
    fclose($fp);
 
    // and output the buffer’s contents to the browser as well
    ob_end_flush();
}
?>
 
I am a PHP newbie - how can I get my HTML and external ebay JAVASCRIPT to render properly?

Re: caching php page works but HTML & javascript not rendering

Posted: Wed Jan 30, 2008 1:14 am
by iacataca
1st look: You provide the "application/x-javascript" content/type, but the content is actually html.

2nd look: If I understand correctly, the load will be on the client's browser, because the browser will retrieve and execute that ebay script. Since your server does not provide that content, it has nothing to cache. What you are trying to cache currently is static html, so I don't know how much it would help to cache it. It could work slowly because writing files could take up time.