Skip to content

Commit 4cc8943

Browse files
committed
Updating purge script
1 parent 30cb681 commit 4cc8943

File tree

4 files changed

+49
-45
lines changed

4 files changed

+49
-45
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "netdnarws-php"]
2+
path = netdnarws-php
3+
url = https://github.com/netdna/netdnarws-php.git

config-sample.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
{
22
"url": "local.codeorigin.jquery.com",
33
"username": "admin",
4-
"password": "secret"
4+
"password": "secret",
5+
"cdn": {
6+
"alias": "jquery",
7+
"consumer_key": "key",
8+
"consumer_secret": "secret",
9+
"zone_id": 1
10+
}
511
}

netdnarws-php

Submodule netdnarws-php added at a0e2b6b

purge.php

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,42 @@
11
<?php
2+
//requires https://github.com/netdna/netdnarws-php
3+
require_once('netdnarws-php/NetDNA.php');
4+
5+
$config = json_decode(readfile('./config.json'), true)["cdn"];
6+
7+
//place your alias, key, secret into this constructor
8+
$api = new NetDNA($config["alias"], $config["consumer_key"], $config["consumer_secret"]);
9+
10+
function purgeCacheFileFromCDN($id, $files = null) {
11+
12+
global $api;
13+
//3 options for purge
14+
$result = null;
15+
16+
if ($files == null){
17+
$result = $api->delete('/zones/pull.json/'.$id.'/cache');
18+
} else if (!is_array($files)){
19+
//Purge single file
20+
$params = array('file'=>$files);
21+
$result = $api->delete('/zones/pull.json/'.$id.'/cache',$params);
22+
} else if (is_array($files)){
23+
//Purge multiple files
24+
$params = array();
25+
foreach ($files as $k=>$v) $params[$k] = $v;
26+
$result = $api->delete('/zones/pull.json/'.$id.'/cache',$params);
27+
}
28+
29+
$result = json_decode($result,true);
30+
if ($result['code'] != 200) {
31+
echo 'Error reported: '.print_r( $result, 1 );
32+
} else {
33+
echo "Done\n";
34+
}
235

3-
function purgeCacheFileFromCDN($urlToPurge, $mediaType) {
4-
5-
$request_params = (object) array(
6-
'MediaPath' => $urlToPurge,
7-
'MediaType' => $mediaType, // MediaType 8=small 3=large
8-
);
9-
$data = json_encode( $request_params );
10-
11-
//## setup the connection and call.
12-
$ch = curl_init();
13-
curl_setopt($ch, CURLOPT_URL, 'https://api.edgecast.com/v2/mcc/customers/1257/edge/purge');
14-
curl_setopt($ch, CURLOPT_PORT, 443);
15-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
16-
curl_setopt($ch, CURLOPT_HEADER, 0);
17-
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
18-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
19-
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
20-
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
21-
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
22-
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
23-
curl_setopt($ch, CURLOPT_TIMEOUT, 45);
24-
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
25-
'Authorization: tok:d1eceb00-7b0b-499f-a9e2-c79379502e88',
26-
'Content-Type: application/json',
27-
'Accept: application/json',
28-
'Content-length: '.strlen($data)
29-
));
30-
$head = curl_exec($ch);
31-
$httpCode = curl_getinfo($ch);
32-
curl_close($ch);
33-
34-
if ($httpCode['http_code'] != 200) {
35-
echo 'Error reported: '.print_r( array( $head, $httpCode ), 1 );
36-
} else {
37-
echo "Done\n";
38-
}
3936
}
4037

41-
// set by nginx
42-
$domain = strtolower( $_SERVER["CDN_HOSTNAME"] );
43-
44-
$type = $domain == "content.jquery.com" ? 3 : 8;
38+
//set zone id that you want to purge
39+
$zone_id = $config["zone_id"];
4540

4641
$parts = preg_split( "/\?reload=?/", $_SERVER["REQUEST_URI"] );
4742
if ( !$parts ) {
@@ -50,9 +45,8 @@ function purgeCacheFileFromCDN($urlToPurge, $mediaType) {
5045
exit;
5146
}
5247

53-
$url = "http://$domain" . $parts[ 0 ];
48+
$files = $parts[0]
5449

5550
header( "Content-Type: text/plain" );
56-
57-
echo "Attempting to purge: $url ";
58-
purgeCacheFileFromCDN( $url, $type );
51+
echo "Attempting to purge: ".$zone_id.": ".$file;
52+
purgeCacheFileFromCDN( $zone_id, $file );

0 commit comments

Comments
 (0)