Home > Enterprise >  How to request API Gateway endpoint using PHP AWS SDK?
How to request API Gateway endpoint using PHP AWS SDK?

Time:04-09

I have https://api-id.execute-api.region.amazonaws.com/stage endpoint, how to query it using SDK?

Found ApiGatewayClient but can't figure out how to use it properly and didn't find any helpful usage examples on the internet.

Some methods require some ConnectionID or another arguments, I am sure there should be a simple way.

EDIT For instance my Api url is https://myawesomeid.execute-api.eu-central-1.amazonaws.com/prod/ where "myawesomeid" is my ApiId. Querying it like this:

$this->client->getStage([
            'restApiId' => 'myawesomeid',
            'stageName' => 'execute-api',
        ]);

And got such error:

Error executing \"GetStage\" on \"https://apigateway.eu-central-1.amazonaws.com/restapis/myawesomeid/stages/execute-api\"; AWS HTTP error: Client error: `GET https://apigateway.eu-central-1.amazonaws.com/restapis/myawesomeid/stages/execute-api` resulted in a `403 Forbidden` response:\n{\"Message\":\"User: arn:aws:iam::804737862755:user/staging-api-s3-assets is not authorized to perform: apigateway:GET on r (truncated...)\n AccessDeniedException (client): User: arn:aws:iam::804737862755:user/staging-api-s3-assets is not authorized to perform: apigateway:GET on resource: arn:aws:apigateway:eu-central-1::/restapis/myawesomeid/stages/execute-api because no identity-based policy allows the apigateway:GET action - {\"Message\":\"User: arn:aws:iam::804737862755:user/staging-api-s3-assets is not authorized to perform: apigateway:GET on resource: arn:aws:apigateway:eu-central-1::/restapis/myawesomeid/stages/execute-api because no identity-based policy allows the apigateway:GET action\"}\n

I think I might not have enough permissions, but using Postman I can query my Api endpoint by this credentials I use to connect via code. What I need is just to make GET/POST to my API. Am I doing it wrong?

CodePudding user response:

First you build client with needed parameters, then you call proper methods. E.g. getStage:

$client = new ApiGatewayClient({
   'credentials' => [
        // This is optional, as AWS SDK will take it directly from _SERVER superglobal
        'key' => $_SERVER['AWS_ACCESS_KEY_ID'],
        'secret' => $_SERVER['AWS_SECRET_ACCESS_KEY'],
   ],
   ...
});

$response = $client->getStage([
    'restApiId' => 'api-id',
    'stageName' => 'execute-api',
]);

$body = json_decode($response->get('Body'), true);

echo $body['cacheClusterStatus'];

  • Related