I tried many different ways, sometimes I got 403 sometimes 401 and sometimes 404 errors but never succeeded.
I want to update the AMP content in the Google cache, I have read and reviewed all the documents, but I could not succeed. The code I wrote is below and now I am getting 404 error.
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$dom = 'https://arthurfrank-xyz.cdn.ampproject.org/';
$update = 'update-cache/c/s/arthurfrank.xyz/amp';
$action = '?amp_action=flush';
$ts = '&_ts='.time();
$save_dir = rtrim(__DIR__, '/') . '/' . '.well-known/amphtml/';
$new_key_pair = openssl_pkey_new(array(
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
));
openssl_pkey_export($new_key_pair, $private_key_pem);
$details = openssl_pkey_get_details($new_key_pair);
$public_key_pem = $details['key'];
openssl_sign($dom . $update, $signature, $private_key_pem, OPENSSL_ALGO_SHA256);
file_put_contents($save_dir . 'apikey.pub', $public_key_pem);
/*file_put_contents($save_dir . 'private_key.pem', $private_key_pem);
file_put_contents($save_dir . 'signature.dat', $signature);*/
//validate the signature
$r = openssl_verify($dom . $update, $signature, $public_key_pem, "sha256WithRSAEncryption");
$signatures = '&_url_signature=' . str_replace(['-----BEGIN PUBLIC KEY-----', '-----END PUBLIC KEY-----'], '',$public_key_pem);
$url = $dom . $update . $action . $ts . ($signatures);
$url = str_replace(["\r","\n","\t",' '], '', $url );
try {
$ch = curl_init();
if ($ch === false) throw new Exception('failed to initialize');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
if ($content === false) throw new Exception(curl_error($ch), curl_errno($ch));
$httpReturnCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
} catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
} finally {
if (is_resource($ch)) curl_close($ch);
}
echo $content;
