Purging EdgeCast CDN Content Using the REST API in PHP or...

Expand / Collapse
Have you just made a change to your site, behind the EdgeCast CDN, and want your changes visible to viewers on your site now and not later?  Are you looking to clear the cache and don’t want this added task of logging in every time you push code changes to the live site?  We know what you're thinking, if only there was an automated way to do this, that would be wonderful.  We have helped make this task easier on you, because we too know how much more simplified this can be.  Below you will find ColdFusion and  PHP scripts for purging the cache utilizing the REST API web service.  You need only to configure the settings in the top of the file, and run the script after pushing code, image, css changes, or anything else you wish to clear from the cache.  Be mindful to test, as errors can be returned if the file is not configured correctly.

<!---
The 4 variables below need to be set based on your account and path to purge.
The token (Web Service REST API token) can be found within your edgeCast
account under My Settings.  The Account number can be found on the
upper right, below the Company Name.  If you are unsure of what the path
might look like, from within your edgeCast account, under the media type
you wish you flush, go to 'My Edge' and drop down the Purge URL field,
these are what you would enter into the variable, with a trailing '/*' sans
quotes to ensure the purge is recursive.  Keep in mind that if you load this
page through the CDN, that it can and will be cached depending upon your
settings.  The Force Purge button causes a POST to the page forcing the
request through the CDN.
--->
<cfset token = ""><!--- Looks like: '51a0000e-1d50-7285-5976-1346a46794d0' --->
<cfset account = ""><!--- Looks like: '0A0A' --->
<cfset mediaPath = ""><!--- Looks like: 'http://wpc.0A0A.edgecastcdn.net/000A0A/example.com/*' --->
<cfset mediaType = "3"><!--- '3' is HTTP Large, '8' is HTTP Small --->
<!--- Do not edit below this line --->
<!DOCTYPE html>
<html>
<body>
<cfif token is not "" AND account is not "" AND mediaPath is not "" AND mediaType is not "">
<cfset token = trim(token)>
<cfset account = trim(account)>
<cfset mediaPath = trim(mediaPath)>
<cfset mediaType = trim(mediaType)>
<cfset purgeURL = "https://api.edgecast.com/v2/mcc/customers/" & account & "/edge/purge">
<cfset objData = {}>
<cfset objData['MediaPath'] = mediaPath>
<cfset objData['MediaType'] = mediaType>
<cfset objData = serializejson(objData)>

<cfhttp url="#purgeURL#" charset="utf-8" method="PUT" result="objHttp">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type = "header" name = "Authorization: tok" value = "#token#" />
<cfhttpparam  type = "body" value = "#objData#">
</cfhttp>

<cfset date = GetHttpTimeString()>
<cfset formatter = CreateObject("java", "java.text.SimpleDateFormat")>
<cfset formatter.init("EEE, dd MMM yyyy HH:mm:ss Z")>
<cfset parsePosition = CreateObject("java", "java.text.ParsePosition")>
<cfset parsePosition.init(0)>
<cfset newDate = formatter.parse(date, parsePosition)>

<cfswitch expression="#objHttp.responseHeader.status_code#">

<cfcase value="503">
</cfcase>
<cfcase value="200">
Success.  Cache for path <cfoutput>#mediaPath#</cfoutput> has been purged.  < br />This message was last updated <cfoutput>#date#<cfoutput/>
<cfdump var="#objHttp.filecontent#"><br>
</cfcase>

<cfdefaultcase>
<cfthrow message="Your call returned an unexpected HTTP status of: #objHttp.responseHeader.status_code#">
</cfdefaultcase>

</cfswitch>


</cfif>
<cfelse>
Error: <br />Not all 4 variables were configured.
</cfif>
<br /><br /><form method="POST"> <input type="submit" name="b1" value="Force Purge." /></form>
</body>
</html>

And of course the PHP version:

<?php
/*
The 4 variables below need to be set based on your account and path to purge.
The token (Web Service REST API token) can be found within your edgeCast
account under My Settings.  The Account number can be found on the
upper right, below the Company Name.  If you are unsure of what the path
might look like, from within your edgeCast account, under the media type
you wish you flush, go to 'My Edge' and drop down the Purge URL field,
these are what you would enter into the variable, with a trailing '/*' sans
quotes to ensure the purge is recursive.  Keep in mind that if you load this
page through the CDN, that it can and will be cached depending upon your
settings.  The Force Purge button causes a POST to the page forcing the
request through the CDN.
*/
$token = "";// Looks like: '51a0000e-1d50-7285-5976-1346a46794d0'
$account = "";// Looks like: '0A0A'
$mediaPath = "";// Looks like: 'http://wpc.0A0A.edgecastcdn.net/000A0A/example.com/*'
$mediaType = "3";// 3 is HTTP Large, 8 is HTTP Small
/* Do not edit below this line */
?><!DOCTYPE html>
<html>
<body><?php
if($token != "" && $account != "" && $mediaPath != "" && $mediaType != "") {
//Setup variables
$token = trim($token);
$mediaPath = trim($mediaPath);
$mediaType = trim($mediaType);
$purgeURL = "https://api.edgecast.com/v2/mcc/customers/" . trim($account) . "/edge/purge";

//Create send data
$request_params = (object) array(
'MediaPath' => $mediaPath,
'MediaType' => $mediaType
);
$data = json_encode($request_params);

//Send the request to Edgecast
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $purgeURL);
curl_setopt($ch, CURLOPT_PORT , 443);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: tok:'.$token,
'Content-Type: application/json',
'Accept: application/json',
'Content-length: '.strlen($data))
);
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch);
curl_close($ch);

//## check if error
if ($httpCode['http_code'] != 200){
print("Error: <br />" . $head);
}
else {
print("Success.  Cache for path ".$mediaPath." has been purged.<br />This message was last updated " . date(DATE_RFC2822) . ".");
}
}
else {
print("Error: <br />Not all 4 variables were configured.");
}
?><br /><br /><form method="POST"> <input type="submit" name="b1" value="Force Purge." /></form>

</body>




Rate this Article:


Details
Category: CDN

Last Modified:Thursday, October 09, 2014 2:07 PM

Type: HOWTO

Level: Beginner

Rated 3 stars based on 2 votes.

Article has been viewed 9,366 times.

Options
Email Article Email Article


Social Bookmarks Social Bookmarks