PHP code example of yanlongli / php-apple-signin

1. Go to this page and download the library: Download yanlongli/php-apple-signin library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yanlongli / php-apple-signin example snippets


use AppleSignIn\AppleSignInDecoder;
use AppleSignIn\ApplePublicKeyProvider;
use AppleSignIn\Exception\InvalidTokenException;

// credential.identityToken (iOS) or the "id_token" field (web)
$identityToken = $_POST['identity_token'] ?? '';

// App bundle ID (native) or Services ID (web) — enables the "aud" check
$clientId = 'com.example.app';

$decoder = new AppleSignInDecoder(new ApplePublicKeyProvider());

try {
    // Third argument: expected nonce, when your auth request used one
    $token = $decoder->decode($identityToken, $clientId /* , $nonce */);
} catch (InvalidTokenException $e) {
    http_response_code(401);
    exit($e->getMessage());
}

$userIdentifier = $token->getUserIdentifier(); // "sub" === credential.user
$email          = $token->getEmail();
$emailVerified  = $token->isEmailVerified();
$privateEmail   = $token->isPrivateEmail();

if (!$token->verifyUser($clientProvidedUser)) {
    // credential.user mismatch
}

use AppleSignIn\ApplePublicKeyProvider;

$provider = new ApplePublicKeyProvider($guzzleClient, $psr16Cache, ttl: 3600);
$decoder  = new AppleSignInDecoder($provider);

use AppleSignIn\ClientSecretGenerator;

$clientSecret = ClientSecretGenerator::generate(
    teamId:     'TEAM1234AB',
    clientId:   'com.example.app',
    keyId:      'KEY1234567',
    privateKey: file_get_contents('/path/to/AuthKey_KEY1234567.p8'),
);

use AppleSignIn\ASDecoder;

$payload = ASDecoder::getAppleSignInPayload($identityToken);
$payload->getEmail();
$payload->getUser();
$payload->verifyUser($clientUser);
bash
composer