forked from timwhitlock/php-varnish
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample-admin.php
More file actions
executable file
·82 lines (56 loc) · 1.77 KB
/
example-admin.php
File metadata and controls
executable file
·82 lines (56 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
* Example use of VarnishAdminSocket class.
* All commands throw an Exception on failure
*/
// plain text output; make like CLI
if( PHP_SAPI !== 'cli' ){
header('Content-Type: text/plain; charset=utf-8');
ini_set('html_errors', 0 );
ob_implicit_flush( 1 );
}
error_reporting( E_ALL | E_STRICT );
// Autoload files using Composer autoload
require_once __DIR__ . '/vendor/autoload.php';
use \PhpVarnish\VarnishAdminSocket;
// open socket connection with your known host, port and version
$Sock = new VarnishAdminSocket( 'localhost', 6082, '3.0.1' );
// secret text from file probably has a trailing newline
$Sock->set_auth("extremelysecrettext\n");
// connect to socket with a timeout parameter
echo 'Connecting ... ';
try {
$Sock->connect(1);
echo "OK\n";
}
catch( \Exception $Ex ){
echo '**FAIL**: ', $Ex->getMessage(), "\n";
exit(0);
}
// Check that child is running. If varnish wasn't running at all, connect would have timed out
$running = $Sock->status();
echo 'Running: ', $running ? 'Yep' : 'Nope', "\n";
// stop it, and check again
if( $running ){
echo "Stopping ... \n";
$Sock->stop();
sleep(1);
$running = $Sock->status();
echo 'Running: ', $running ? 'Yep' : 'Nope', "\n";
}
// start it up again, and check
echo "Starting ... \n";
$Sock->start();
sleep(1);
$running = $Sock->status();
echo 'Running: ', $running ? 'Yep' : 'Nope', "\n";
// purge your home page
$Sock->purge_url('^/$');
// purge by a more complex rule
$Sock->purge('req.url ~ ^/$ && req.http.host ~ example\\\\.com$');
// show purge list
echo "Getting purge list ...\n";
$list = $Sock->purge_list();
var_dump( $list );
// exit gracefully, quits CLI and closes socket
$Sock->quit();