Clearing the Cache of a Site Identity

We recommend that you leverage a URI template parser to manage URI templates but you can also use a simple search and replace for this as shown in the samples in this section. The samples below assume that you are authenticated.

The following code is written in PHP and assumes that the client already knows the Site ID and Site Identity ID where it intends to purge the cache:

  1. The client goes to the discovery page, finds the “cachepurge” resource URI, fills in the necessary IDs, makes a POST request to create a Cache Purge job, and prints the URL of the Cache Purge job resource on the screen.
<?php

//function for making cURL requests
function curl($url,$method,$data){

//cURL call
$ch = curl_init();

if($method){
curl_setopt ($ch, CURLOPT_POST, true);
if($data){
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
}
}

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-API-TOKEN: YOUR-API-TOKEN"));

$output = curl_exec($ch);

if($output !== false || curl_error($ch)) {

$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return array($status,$output);

curl_close($ch);
}

}

//list of known variables
$site_id = 130;
$campaign_id = 159;

//call the discovery page
$discovery = "http://admin1.sitespect.com/api"";

//Find the cachepurge resource URI from the discovery page, parse the URI template and create the cache purge job,
and print the cache purge job resource on the screen
$request = curl($discovery);

if ($request[0] == 200){
$json = json_decode($request[1],true);

$siteidentities = $json['_links']['siteidentities']["href"];

//URI Template parsing using a simple str_replace function instead of a URI Template parsing library
$cachepurge = str_replace('{site-id}', $site_id, $cachepurge);
$cachepurge = str_replace('{siteidentity-id}', $campaign_id, $cachepurge);

}


if($cachepurge){
$request = curl($cachepurge,"POST"," "); //POST request

//we expect to get a created cache purge job, meaning status code 201
if($request[0] == 201){

$json = json_decode($request[1],true);

//print the cache purge job URL on the screen
echo $json['_links']['self']["href"];

}

}

?>