PHP code example of pyaesoneaung / aws-msk-iam-sasl-signer-php

1. Go to this page and download the library: Download pyaesoneaung/aws-msk-iam-sasl-signer-php 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/ */

    

pyaesoneaung / aws-msk-iam-sasl-signer-php example snippets


use PyaeSoneAung\AwsMskIamSaslSigner\AwsMskIamSaslSigner;

$awsMskIamSaslSigner = new AwsMskIamSaslSigner(
    'us-east-1', // region
    'testAccessKeyId', // iam access key id
    'testSecretAccessKey' // iam secret access key
);
$token = $awsMskIamSaslSigner->generateToken();

// [
//     'token' => 'aHR0cHM6Ly9rYWZrYS5hcC1zb3V0aGVhc3QtMS5hbWF6b25hd3MuY29tLz9BY3Rpb249a2Fma2EtY2x1c3RlciUzQUNvbm5lY3QmVXNlci1BZ2VudD1hd3MtbXNrLWlhbS1zYXNsLXNpZ25lci1waHAlMkYxLjAuMCZYLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPXRlc3RBY2Nlc3NLZXlJZCUyRjIwMjQwMTE0JTJGYXAtc291dGhlYXN0LTElMkZrYWZrYS1jbHVzdGVyJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDAxMTRUMTIyNTQ5WiZYLUFtei1FeHBpcmVzPTkwMCZYLUFtei1TaWduYXR1cmU9NWUxYzY4YzI5NDRkN2I2NjY0ZDkyMTJkMGJlMDQ1NTYyYzc5Y2U0NTZhNGJjZjg2YTQ3NTk3NDcxMjI3NTY3YyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3Q',
//     'expiryTime' => 1705236049000
// ]

use PyaeSoneAung\AwsMskIamSaslSigner\AwsMskIamSaslSigner;
use SimpleKafkaClient\Configuration;
use SimpleKafkaClient\Producer;

$conf = new Configuration();
$conf->set('metadata.broker.list', 'kafka:9092');
$conf->set('security.protocol', 'SASL_SSL');
$conf->set('sasl.mechanisms', 'OAUTHBEARER');

$producer = new Producer($conf);

$awsMskIamSaslSigner = new AwsMskIamSaslSigner(
    'us-east-1',
    'testAccessKeyId',
    'testSecretAccessKey'
);
$token = $awsMskIamSaslSigner->generateToken();
$producer->setOAuthBearerToken($token['token'], $token['expiryTime'], 'principalClaimName=azp');

$topic = $producer->getTopicHandle('topic-name');

$topic->producev(
    RD_KAFKA_PARTITION_UA,
    RD_KAFKA_MSG_F_BLOCK,
    'value',
    'key',
    ['header-key' => 'header-value']
);

$producer->poll(0);

$result = $producer->flush(20000);