|
1 | 1 | <?php
|
2 |
| -//requires https://github.com/netdna/netdnarws-php |
3 |
| -require_once('netdnarws-php/NetDNA.php'); |
| 2 | +require_once( 'netdnarws-php/NetDNA.php' ); |
4 | 3 |
|
5 |
| -$config = json_decode(file_get_contents('./config.json'), true); |
6 |
| -$config = $config["cdn"]; |
| 4 | +$config = json_decode( file_get_contents( './config.json' ), true ); |
| 5 | +$config = $config[ 'cdn' ]; |
| 6 | +$zone_id = 1; |
7 | 7 |
|
8 |
| -//place your alias, key, secret into this constructor |
9 |
| -$api = new NetDNA($config["alias"], $config["consumer_key"], $config["consumer_secret"]); |
| 8 | +$parts = preg_split( '/\?reload=?/', $_SERVER[ 'REQUEST_URI' ] ); |
| 9 | +if ( !$parts ) { |
| 10 | + header( '400 Bad Request' ); |
| 11 | + echo $_SERVER[ 'REQUEST_URI' ] . ' is not a valid URI.'; |
| 12 | + exit; |
| 13 | +} |
10 | 14 |
|
11 |
| -function purgeCacheFileFromCDN($id, $files = null) { |
| 15 | +$file = $parts[ 0 ]; |
12 | 16 |
|
13 |
| - global $api; |
14 |
| - //3 options for purge |
15 |
| - $result = null; |
16 |
| - |
17 |
| - if ($files == null){ |
18 |
| - $result = $api->delete('/zones/pull.json/'.$id.'/cache'); |
19 |
| - } else if (!is_array($files)){ |
20 |
| - //Purge single file |
21 |
| - $params = array('file'=>$files); |
22 |
| - $result = $api->delete('/zones/pull.json/'.$id.'/cache',$params); |
23 |
| - } else if (is_array($files)){ |
24 |
| - //Purge multiple files |
25 |
| - $params = array(); |
26 |
| - foreach ($files as $k=>$v) $params[$k] = $v; |
27 |
| - $result = $api->delete('/zones/pull.json/'.$id.'/cache',$params); |
28 |
| - } |
| 17 | +header( 'Content-Type: text/plain' ); |
| 18 | +echo "Attempting to purge: $zone_id: $file.\n"; |
29 | 19 |
|
30 |
| - $result = json_decode($result,true); |
31 |
| - if ($result['code'] != 200) { |
32 |
| - echo 'Error reported: '.print_r( $result, 1 ); |
33 |
| - } else { |
34 |
| - echo "Done\n"; |
35 |
| - } |
| 20 | +$api = new NetDNA( 'jquery', $config[ 'consumer_key' ], $config[ 'consumer_secret' ] ); |
36 | 21 |
|
| 22 | +if ( empty( $file ) ) { |
| 23 | + // Purge all files |
| 24 | + $result = $api->delete( '/zones/pull.json/' . $zone_id . '/cache' ); |
| 25 | +} else { |
| 26 | + // Purge single file |
| 27 | + $result = $api->delete( '/zones/pull.json/' . $zone_id . '/cache', array( 'file' => $file ) ); |
37 | 28 | }
|
38 | 29 |
|
39 |
| -//set zone id that you want to purge |
40 |
| -$zone_id = $config["zone_id"]; |
41 |
| - |
42 |
| -$parts = preg_split( "/\?reload=?/", $_SERVER["REQUEST_URI"] ); |
43 |
| -if ( !$parts ) { |
44 |
| - header("400 Bad Request"); |
45 |
| - echo $_SERVER["REQUEST_URI"]." is not a valid URI"; |
46 |
| - exit; |
| 30 | +$result = json_decode( $result, true ); |
| 31 | +if ( $result[ 'code' ] !== 200 ) { |
| 32 | + echo 'Error reported: ' . print_r( $result, true ); |
| 33 | +} else { |
| 34 | + echo 'Done\n'; |
47 | 35 | }
|
48 |
| - |
49 |
| -$file = $parts[0]; |
50 |
| - |
51 |
| -header( "Content-Type: text/plain" ); |
52 |
| -echo "Attempting to purge: ".$zone_id.": ".$file."\n"; |
53 |
| -purgeCacheFileFromCDN( $zone_id, $file ); |
0 commit comments