Created
April 30, 2019 07:50
-
-
Save trabulium/be1cde1d3615db09694bb7aa270625c1 to your computer and use it in GitHub Desktop.
php script to Bulk Update AMP cache to clear AMP urls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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&_action=flush&_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."&_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