Skip to content

Instantly share code, notes, and snippets.

@krzysztofbukowski
Last active December 30, 2020 11:02
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save krzysztofbukowski/739ccf4061d69360b5b2c8306f5878bd to your computer and use it in GitHub Desktop.
Save krzysztofbukowski/739ccf4061d69360b5b2c8306f5878bd to your computer and use it in GitHub Desktop.
Amp cache url PHP
<?php
function urlsafe_b64encode($string) {
return str_replace(array('+','/','='),array('-','_',''), base64_encode($string));
}
$timestamp=time();
$ampBaseUrl = "https://www-domain-com.cdn.ampproject.org";
$signatureUrl = '/update-cache/c/s/www.domain.com/amp/page-url?amp_action=flush&amp_ts='.$timestamp;
// opening the private key
$pkeyid = openssl_pkey_get_private("file://amp-private-key.pem");
// generating the signature
openssl_sign($signatureUrl, $signature, $pkeyid, OPENSSL_ALGO_SHA256);
openssl_free_key($pkeyid);
// urlsafe base64 encoding
$signature = urlsafe_b64encode($signature);
// final url for updating
$ampUrl = $ampBaseUrl.$signatureUrl."&amp_url_signature=".$signature;
echo $ampUrl."\n";
@seomaz
Copy link

seomaz commented Jun 11, 2019

@krzysztofbukowski Dont work for me... i get always "URL signature verification error"

@seomaz
Copy link

seomaz commented Jun 11, 2019

@krzysztofbukowski Add this line: $ampUrl = str_replace("&", "&amp", $ampUrl); before echo $ampUrl."\n";

work now!

@ozer933
Copy link

ozer933 commented Sep 16, 2019

Dont work for me...
openssl_sign(): supplied key param cannot be coerced into a private key in

@thomas-wisc
Copy link

@krzysztofbukowski The value of $signature is empty for me.

I generated a private key, and placed in the same directory as the PHP script. In fact, I coded
$pkeyid = openssl_pkey_get_private("/full/path/to/private-key.pem");

If I echo the value of $signature, it's empty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment