如何使用AWS PHP SDK访问API网关终端节点

时间:2018-10-12 14:06:32

标签: php aws-sdk aws-api-gateway

我需要将数据发布到AWS API Gateway URL。

我不知道如何使用PHP做到这一点。 (就像我无法想象的那样困难。)

任何帮助将不胜感激。

我需要将JSON正文发送到API网关API(IAM),SDK似乎没有任何文档可以帮助我。

我需要发布:

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

使用sig 4到达API网关端点 端点示例(https://myendpoint.com/api

2 个答案:

答案 0 :(得分:0)

您可以通过作曲家composer require aws/aws-sdk-php安装AWS php sdk,这是github https://github.com/aws/aws-sdk-php。如果您想做一些简单的事情,或者他们没有您想要的东西,可以在PHP中使用curl发布数据。

$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);

答案 1 :(得分:0)

我真的很挣扎,最后设法通过以下方法清除它:

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());

此示例假设您从 EC2 或具有允许访问此 API 网关组件和 ./aws 目录中的 AWS PHP 开发工具包的实例配置文件运行。