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

发布于 2025-01-19 21:16:50 字数 1695 浏览 0 评论 0原文

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

找到ApigatewayClient,但无法弄清楚如何正确使用它,并且在Internet上找不到任何有用的用法示例。

某些方法需要一些连接或其他参数,我相信应该有一种简单的方法。

编辑 例如,我的API URL是https://myawsomeid.execute-api.eu-central-central-1.amazonaws.com/prod/其中“ myawsomeid”是我的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端点。 我需要的只是使我的API获取/发布。我做错了吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗恋未遂 2025-01-26 21:16:50

首先,您使用需要参数,然后调用正确的方法。例如

$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'];

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'];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文