WeatherKit REST API Invalid Signature (php)

I'm trying to make a PHP implementation to fetch data from the WeatherKit REST API.

  • When using jwt.io to create a JWT, everything works correctly, so my keys, identifiers, etc. are set up correctly.
  • When using jtw.io to verify my own generated JWT, the header and payload are correct, but "Invalid Signature" is displayed.

The file path for the private key exists and the file is loaded correctly.

My PHP code:

       function generate_jwt(): String {
            $tmpFilePath = realpath(dirname(__FILE__)).'/';
            $filePath = $tmpFilePath.'../AuthKey.pem';
            //$filePath = $tmpFilePath.'../AuthKey.p8';
            $private_key = NULL;
            
            if (file_exists($filePath)) {
                $private_key = file_get_contents($filePath);
            }
            
            $header = [
                "alg" => "ES256",
                "kid" => "XXXXXXXXXX",
                "id" => "YYYYYYYYYY.com.thing.stuff",
                "typ" => "JWT"
            ];
            $header = $this->base64_url_encode(json_encode($header));
            
            $issuedAt = time();
            $payload = [
                "iat" => $issuedAt,
                "exp" => $issuedAt + 30,
                "iss" => "YYYYYYYYYY",
                "sub" => "com.thing.stuff"
            ];
            $payload = $this->base64_url_encode(json_encode($payload));

            $signature = $this->base64_url_encode(hash_hmac('sha256', "$header.$payload", $private_key, true));
            $jwt = "$header.$payload.$signature";
            
            return $jwt;
        }

        function base64_url_encode($text): String {
            return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($text));
        }

Any ideas?