【问题标题】:Why Cloudfront returning 403 with Guzzle?为什么 Cloudfront 会使用 Guzzle 返回 403?
【发布时间】:2021-03-31 16:56:49
【问题描述】:

我正在使用一个使用 CloudFront 服务的 API,

我在使用 Guzzle 的请求时收到 403 错误,但如果使用例如 PHP Curl 或通过 Postman 或浏览器调用有效。

这里是 Guzzle 的日志: Log of guzzle

这里是部分代码:

/**
 * @return void
 */
public function __construct()
{
    $this->client = new Client([
        'base_uri' => env('API_HOST'),
        'headers'  => [
            'Accept' => 'application/json',
            'Content-Type' => 'application/json',
        ],
        'timeout' => 30,
        'debug' => true,
    ]);
}

/**
 * @param string $method
 * @param string $url
 * @param array $body
 * @param bool $isMultipart
 *
 * @return ResponseInterface
 */
private function request(string $method, string $url, array $body = [], bool $isMultipart = false): ResponseInterface
{
    if ($isMultipart) {
        $params['multipart'] = [$body];
    } else {
        $params['json'] = $body;
    }

    $url = $this->appedAuthTokensToUrl($url);

    return $this->client->request($method, $url, $params);
}

【问题讨论】:

  • 嗨,您可以使用代码块将代码/日志作为文本发布吗?有关更多信息,请参阅此stackoverflow.com/help/minimal-reproducible-example
  • 好的,所以我将代码添加为文本,但是stackoverflow不允许我发布大量代码
  • 也许可以尝试将其修剪到相关部分,或者至少在 pastebin 或易于搜索的东西而不是图像中。

标签: php amazon-cloudfront guzzle


【解决方案1】:

终于找到了解决办法。如果 GET 请求中有正文,Cloudfront 将返回 403。

所以在我的情况下,问题是如果(总是设置一个主体):

       if ($isMultipart) {
            $params['multipart'] = [$body];
        } else {
            $params['json'] = $body;
        }

Here link of Cloudfront docs

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-26
    • 2021-04-21
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 2019-11-17
    • 2019-10-04
    • 2011-10-30
    相关资源
    最近更新 更多