Last active
July 2, 2020 16:52
-
-
Save vanduc1102/d1e9f998adc0340c6702ea2dc298500c to your computer and use it in GitHub Desktop.
AMP update cache, followup the google tutorial: https://developers.google.com/amp/cache/update-cache#call-the-update-cache
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 | |
require __DIR__ . '/vendor/autoload.php'; | |
# https://github.com/phpseclib/phpseclib | |
use phpseclib\Crypt\RSA; | |
$update_cache_url = $_GET['url'] ?? $_GET['link'] ?? ''; | |
if( empty ($update_cache_url) || strpos($update_cache_url, 'https://www.sample.com.au/') === false ) { | |
die("Please provide a valid url, given: " . $update_cache_url); | |
} | |
$private_key_path = __DIR__ . 'private-key.pem'; | |
$rsa = new RSA(); | |
$rsa->loadKey($private_key_path); | |
$plaintext = '/update-cache/c/s/' . $update_cache_url . '?amp_action=flush&_ts=' . time() ; | |
$rsa->setSignatureMode(RSA::SIGNATURE_PKCS1); | |
$signature = $rsa->sign($plaintext); | |
$base64_signature = base64url_encode( $signature ); | |
$amp_request_update_url = 'https://www-sample-com-au.cdn.ampproject.org/c/s/' . $update_cache_url . $plaintext . '&_url_signature='.$base64_signature; | |
$response = get_response( $amp_request_update_url); | |
echo json_encode( $response ); | |
exit(0); | |
function get_response($url) { | |
// init curl object | |
$curl_object = curl_init(); | |
// define options | |
$options = array( | |
CURLOPT_URL => $url, | |
CURLOPT_RETURNTRANSFER => true, | |
); | |
// apply those options | |
curl_setopt_array($curl_object, $options); | |
// execute request and get response | |
$result = curl_exec($curl_object); | |
return $result; | |
} | |
function base64url_encode($data) { | |
return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment