speeddemon786 / aws4-signature-php

A demo to calculate signature to access secured API using PHP and cURL.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Amazon API Gateway & Authentication

Access using PHP

Here trying to demonstrate a that how to calculate signature for AWS Authorization.

I refer this link to implement the steps as describe https://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html

How to use?

Step 1: Include the file where calculation and cURL implemented within two function.
include('./aws_secure_request.php');
Step 2: Set the values in configuration variable.
// Configuration values
$host = '<your host name>';
$accessKey = '<access key>';
$secretKey = '<secret key>';
$region = '<region>';
$service = '<service>';

$requestUrl = '<full url>'; $uri = '<method path>'; $httpRequestMethod = '<http verb>';

For example
$region = 'ap-southeast-1'
$service = 'execute-api'
$requestUrl = 'https://host-domain/object/identifier'
$uri = '/object/identifier'
$httpRequestMethod = 'PUT'

Step 3: Prepare data to send to API.
$data = json_encode(array(
    "username" => "sample-demo",
    "firstName" => "Sample",
    "lastName" => "Demo",
    "date_of_birth" => "1999-05-08"
));
Step 4: Now call the method to generate signature get all headers need to send.
$headers = 
    calcualteAwsSignatureAndReturnHeaders(
        $host, $uri, $requestUrl, 
        $accessKey, $secretKey, $region
        $service, $httpRequestMethod, 
        $data, TRUE);
Step 5: Final step to call API using cURL.
$result = 
    callToAPI(
        $requestUrl, $httpRequestMethod, 
        $headers, $data, TRUE);
Note: Last parameter as TRUE in above two function can be use to show DEBUG information, otherwise just send FALSE.

You can use aws_request_sample.php to start.

About

A demo to calculate signature to access secured API using PHP and cURL.


Languages

Language:PHP 100.0%