Clearing the Cache of a Site Identity Without Knowing the Site ID nor the Site Identity

The following code is written in PHP and assumes that the client knows only the domain and protocol of the Site Identity where it intends on purging the cache:

  1. The client goes to the discovery page and retrieves all Site Identities through the “siteidentites” link relation.
  2. The client parses the returned JSON, which includes embedded individual Site Identities, and finds the Cache Purge resource through the “cachepurge” link relation of the Site Identity that matches the correct domain and protocol.
  3. The client creates a cache purge job by making a POST request to the Cache Purge resource and grabs the URL of the cache purge job from the response (also available in the Location header) and prints this URL on the screen. At this point anyone can hit the cache purge job URL and be notified if the cache is still processing (Status Code 202 and processing message in JSON response) or if the cache purge is complete (status code 200 and completed message in JSON response).
<?php

//functuion 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
$protocol = "http";
$ip = "100.100.100.100";

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

//Part 1 and 2: Client makes an API request to discover page, finds the all site identities resource,
calls it and finds the correct site identity and its cachepurge resource
$request = curl($discovery);

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

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

if($siteidentities){

$request = curl($siteidentities);

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

//how many site identies
$length = count($json["_embedded"]);

for($i=0;$i<$length;$i++){

//find the site identity that matches ip and protocol
if($json["_embedded"][$i]["BackEndIPAddress"] == $ip && $json["_embedded"][$i]["BackEndProtocol"] ==
$protocol){
$cachepurge = $json["_embedded"][$i]["_links"]["cachepurge"]["href"];
}
}
}
}
}

//Part 3: Client creates a cache purge job and prints the cache purge job id URL on the screen to be checked for progress

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"];

}

}
?>