1
1
<?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
+ }
2
35
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
- }
39
36
}
40
37
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 " ];
45
40
46
41
$ parts = preg_split ( "/\?reload=?/ " , $ _SERVER ["REQUEST_URI " ] );
47
42
if ( !$ parts ) {
@@ -50,9 +45,8 @@ function purgeCacheFileFromCDN($urlToPurge, $mediaType) {
50
45
exit ;
51
46
}
52
47
53
- $ url = " http:// $ domain " . $ parts [ 0 ];
48
+ $ files = $ parts [0 ]
54
49
55
50
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