Skip to content

Instantly share code, notes, and snippets.

@mtougeron
Last active December 16, 2015 06:19
Show Gist options
  • Save mtougeron/5390562 to your computer and use it in GitHub Desktop.
Save mtougeron/5390562 to your computer and use it in GitHub Desktop.
<?php
include './guzzle.phar';
use Guzzle\Http\Client;
// Create the client with the curl options for verbose
$client = new Client('', array('curl.options' => array(CURLOPT_VERBOSE=>true)));
// create a PURGE request to send
$request = $client->createRequest('PURGE', 'http://127.0.0.1:81/?foo=bar&abc=123');
// Set the Host header
$request->setHeader('Host', 'www.ign.com');
// send the request
$request->send();
$> php ./guzzle-test.php
* About to connect() to www.ign.com port 81 (#0)
what I want it to do is act like:
$> telnet 127.0.0.1 81
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
PURGE /?foo=bar&abc=123 HTTP/1.1
Host: www.ign.com
Content-Type: text/plain
It looks like whenever you change/set the Host header in guzzle it changes what host it is trying to
connect to instead of connecting to the original url and sending a different Host header.
<?php
class Oyster_Service_Varnish
{
var $_varnishHosts = array(
'oyster-varnish-01',
'oyster-varnish-02',
);
public function setVarnishHosts(array $hosts)
{
$this->_varnishHosts = $hosts;
return $this;
}
function purge($url)
{
if ( empty($url) ) {
throw new Exception('ERROR: No url specified.');
} elseif ( strpos($url, '://') !== false ) {
throw new Exception('Do not include the scheme. Just use the url path. e.g., / or /articles.');
}
$redirectHelper = new Oyster_Controller_Action_Helper_Redirect();
$client = new Zend_Http_Client();
$domains = array();
// Create a list of domains that we need to purge
foreach ( $redirectHelper->getSupportedEnglishLocales() as $locale => $domain ) {
$domains[$domain . '.ign.com'] = $locale;
if ( $locale == $redirectHelper->getDefaultLocale() ) {
$domains['m.ign.com'] = $locale;
} else {
$domains['m.' . $domain . '.ign.com'] = $locale;
}
}
foreach ( $domains as $domain => $locale ) {
$host = strtolower($domain) . '.ign.com';
// Reset everything for the next set of requests
$client->resetParameters(true);
// We're sending a PURGE request
$client->setMethod('PURGE');
// Make sure the correct Host header is sent
$client->setHeaders('Host', $host);
// Configure the cookies
$client->setCookie(
$redirectHelper->getI18nCcPrefCookieName(),
$redirectHelper->createI18nCcPrefCookieValue(strtoupper($locale))
);
// Loop through each varnish host and send the request
foreach ($this->_varnishHosts as $varnishHost) {
$client->setUri('http://' . $varnishHost . $url);
$client->request();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment