• Resolved masato01

    (@masato01)


    Hello!
    I’m really in trouble. How do I implement the following code in WordPress?

      Where and how to edit in the code?
      What file name and extension should I save?
      Where should I save it?

    function generateURL( $page ) {
    $domain = ‘example.com’;
    $url = $domain . $page;

    $timestamp = time();
    $ampBaseUrl = “https://” . str_replace( array( ‘-‘, ‘.’ ), array(‘–‘, ‘-‘ ), $domain ) . “.cdn.ampproject.org”;
    $signatureUrl = ‘/update-cache/c/s/’ . $url . ‘?amp_action=flush&amp_ts=’ . $timestamp;

    // opening the private key
    $pkeyid = openssl_pkey_get_private( “file:///Users/boofoo/private-key.pem” );
    if ( $pkeyid == false ) {
    echo ‘could not open the private key.’;
    exit;
    }

    // generating the signature
    openssl_sign( $signatureUrl, $signature, $pkeyid, OPENSSL_ALGO_SHA256 );
    openssl_free_key( $pkeyid );

    // urlsafe base64 encoding
    $signature = $this->urlsafe_b64encode( $signature );

    // final url for updating
    $ampUrl = $ampBaseUrl . $signatureUrl . “&amp_url_signature=” . $signature;

    return $ampUrl;

    }

    function urlsafe_b64encode( $string ) {
    return str_replace( array( ‘+’, ‘/’, ‘=’ ), array( ‘-‘, ‘_’, ” ), base64_encode( $string ) );
    }

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘About PHP implementation for AMP cache’ is closed to new replies.