Last active
December 30, 2020 11:02
-
-
Save krzysztofbukowski/739ccf4061d69360b5b2c8306f5878bd to your computer and use it in GitHub Desktop.
Amp cache url PHP
This file contains hidden or 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)); | |
} | |
$timestamp=time(); | |
$ampBaseUrl = "https://www-domain-com.cdn.ampproject.org"; | |
$signatureUrl = '/update-cache/c/s/www.domain.com/amp/page-url?amp_action=flush&_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."&_url_signature=".$signature; | |
echo $ampUrl."\n"; |
@krzysztofbukowski Add this line: $ampUrl = str_replace("&", "&", $ampUrl);
before echo $ampUrl."\n";
work now!
Dont work for me...
openssl_sign(): supplied key param cannot be coerced into a private key in
@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
@krzysztofbukowski Dont work for me... i get always "URL signature verification error"