php - 如何使用 AWS PHP SDK 访问 API 网关终端节点
问题描述
我需要将数据发布到 AWS API Gateway URL。
我不知道如何用 PHP 做到这一点。(就像我无法想象这会如此困难。)
任何帮助,将不胜感激。
我需要将 JSON 正文发送到 API Gateway API (IAM),SDK 似乎没有任何可以帮助我的文档。
我需要发布这个:
{
"entity": "Business",
"action": "read",
"limit": 100
}
使用 sig 4 示例端点 ( https://myendpoint.com/api
)到 API 网关端点
解决方案
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.
推荐阅读
- android - 如何在 Splashscreen API 中删除我的启动画面图标周围的圆形遮罩?
- java - 通过 jpackage 打包找不到或加载主类
- c - C:在动态分配的内存中插入比初始化更多的元素
- html - 在卡片引导程序中保持相同的内容高度
- php - 让 PHP 路由库与 Azure 应用服务一起使用?
- vuejs2 - 在 vue.js 中自定义 Vuetify 深色主题背景的背景(不是 Nuxt!)
- javascript - 如何解决 VSC 错误“ENOENT:没有这样的文件或目录”?
- python - Tkinter - 如何动态调整渐变帧的大小?
- excel - Excel VBA 2016 / 365 兼容性问题?
- javascript - javascript:如何使 youtube 上的所有链接在新标签页中打开