Page 1 of 1

PHP and Edge Side Includes (ESI)

Posted: Tue Nov 07, 2006 12:28 pm
by guarriman
Hi.

I want to accelerate my webpages by using Akamai and ESI:
http://www.akamai.com/html/support/esi.html

but I don't know how to execute ESI directives with my Apache+PHP webserver.

Any suggestion?

Posted: Wed Nov 08, 2006 6:12 am
by guarriman
I also tried with an Apache module, but it doesn't exist :(

re: ESI and PHP

Posted: Fri Nov 10, 2006 5:02 am
by cantos
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Start by hand coding the ESI in HTML pages so you can see/learn how it works, and use the Akamai ESI debugging tools via the EdgeSuite Test Server to understand what the ESI does. Only once you understand the ESI properly you can safely start writing PHP to drive it.

You'll need to emit the ESI in the output from the PHP: for example create a function called "ESI_include_file($filename)" and from that emits the necessary ESI code. Then, whenever you need to do and edge-include you can call that function from your PHP.

Here is an example building block function ([u][b]not [/b][/u]all that you'll need!) that emits some ESI. Don't try using this as-is, but refer to it once you understand the ESI that you're trying to emit.

--paul

Code: Select all

function make_esi_include_directive($inc_url, $cachedirective, $locstr, $inc_rel_url)
	{
		
		$alt_url = "alternative_web_page.html";
		$html =  "\n<!-- Start {$locstr} URL included using ESI: '$inc_url', '$cachedirective' -->\n";
		if ($inc_rel_url > '')
				$html .=  "\n<!-- alt URL is: '$alt_url', '$cachedirective' -->\n";		$html .= "<esi:try><esi:attempt>\n";
		$html .= "<esi:include src=\"" . $inc_url . "\" ";
		if ($inc_rel_url > '')
			$html .= " alt=\"" . $alt_url . "\" ";
		$html .= "dca=\"esi\" {$cachedirective} />\n";
		$html .= "</esi:attempt><esi:except>\n";
		$html .= "<esi:assign name=\"incFileName\">'$inc_url'</esi:assign>\n";
		$html .= "<esi:assign name=\"cacheDirective\">'$cachedirective'</esi:assign>\n";
		$html .= "<esi:assign name=\"locStr\">'$locstr'</esi:assign>\n";
		$html .= "<esi:include src=\"" . ESI_FAILURE_URL . "\" onerror=\"continue\" " 
					. "dca=\"esi\" {$cachedirective} />\n";
		$html .= "</esi:except></esi:try>\n";
		$html .= "\n<!-- End {$locstr} URL included using ESI: '$inc_url' -->\n";
		return $html;
	}

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Nov 10, 2006 10:23 am
by RobertGonzalez
Try reading the code samples and developers guidelines that are on the page you linked to. There are loads of information available to you there.