如何使用 PHP AWS SDK 请求 API 网关端点?

How to request API Gateway endpoint using PHP AWS SDK?

我有https://api-id.execute-api.region.amazonaws.com/stage端点,如何使用SDK查询?

找到 ApiGatewayClient 但不知道如何正确使用它,也没有在互联网上找到任何有用的用法示例。

有些方法需要一些 ConnectionID 或其他参数,我相信应该有一个简单的方法。

编辑 例如,我的 Api url 是 https://myawesomeid.execute-api.eu-central-1.amazonaws.com/prod/,其中“myawesomeid”是我的 ApiId。 像这样查询:

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

出现这样的错误:

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

我想我可能没有足够的权限,但使用 Postman 我可以通过我用来通过代码连接的凭据查询我的 Api 端点。 我需要的只是让 GET/POST 变成我的 API。我做错了吗?

首先你用 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'];