Skip to content

Instantly share code, notes, and snippets.

@trabulium
Created April 30, 2019 07:50
Show Gist options
  • Save trabulium/be1cde1d3615db09694bb7aa270625c1 to your computer and use it in GitHub Desktop.
Save trabulium/be1cde1d3615db09694bb7aa270625c1 to your computer and use it in GitHub Desktop.
php script to Bulk Update AMP cache to clear AMP urls
<?php
function urlsafe_b64encode($string) {
return str_replace(array('+','/','='),array('-','_',''), base64_encode($string));
}
$ampBaseUrl="https://www-example-com-au.cdn.ampproject.org";
$file = fopen("urls.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
//$signatureUrl = '/update-cache/c/s/www.example.com.au/some-url.html?amp=1&amp_action=flush&amp_ts='.time();
$signatureUrl = trim(fgets($file)).time();
// opening the private key
$pkeyid = openssl_pkey_get_private("file://private-key.pem");
// generating the signature
openssl_sign($signatureUrl, $signature, $pkeyid, OPENSSL_ALGO_SHA256);
// urlsafe base64 encoding
$signature = urlsafe_b64encode($signature);
// final url for updating
$ampUrl = $ampBaseUrl.$signatureUrl."&amp_url_signature=".$signature;
echo $ampUrl . "\n";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $ampUrl);
$result = curl_exec($curl);
echo "\n";
}
fclose($file);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment