2

I need to POST data to an AWS API Gateway URL.

I have no clue how to do this with PHP. (Like I cannot imagine it to be this difficult.)

Any help would be appreciated.

I need to send a JSON body to an API Gateway API (IAM) the SDK does not seem to have any documentation that can help me.

I need to POST this:

{
    "entity": "Business",
    "action": "read",
    "limit": 100
}

To an API gateway endpoint using sig 4 Example endpoint (https://myendpoint.com/api)

2 Answers 2

3

I really struggled with this and finally managed to clear it with the following approach:

require './aws/aws-autoloader.php';

use Aws\Credentials\Credentials;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use Aws\Signature\SignatureV4;
use Aws\Credentials\CredentialProvider;

$url = '<your URL>';
$region = '<your region>';
$json = json_encode(["Yourpayload"=>"Please"]);

$provider = CredentialProvider::defaultProvider();
$credentials = $provider()->wait();
# $credentials = new Credentials($access_key, $secret_key); # if you do not run from ec2
$client = new Client();
$request = new Request('POST', $url, [], $json);
$s4 = new SignatureV4("execute-api", $region);
$signedrequest = $s4->signRequest($request, $credentials);
$response = $client->send($signedrequest);
echo($response->getBody());

This example assumes you are running from an EC2 or something that has an instance profile that is allowed to access this API gateway component and the AWS PHP SDK in the ./aws directory.

Sign up to request clarification or add additional context in comments.

Comments

0

You can install AWS php sdk via composer composer require aws/aws-sdk-php and here is the github https://github.com/aws/aws-sdk-php . In case you want to do something simple or they don't have what you are looking for you can use curl in php to post data.

$ch = curl_init();

$data = http_build_query([
    "entity" => "Business",
    "action" => "read",
    "limit" => 100
]);

curl_setopt_array($ch, [
    CURLOPT_URL => "https://myendpoint.com/api",
    CURLOPT_FOLLOWLOCATION => true
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $data
]);

$response = curl_exec($ch);
$error = curl_error($ch);

3 Comments

You can't do a curl, you need to use sig4. The sdk doesn't provide any information regarding posting to API gateway, hence my question here...
Curl can pretty much do anything if curl can't do what you are looking for you should start looking for an alternative language to use that uses something different. Pass auth headers needed. Ex: curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: AWS4-HMAC-SHA256 ahsadhaash"]);
I need someone who has used AWS's api gateway to answer this question :) thanks for the help, but your answer is not in the ballpark, unfortunately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.