为什么 GuzzleHttp 尝试将端口 80 用于 https?

Why is GuzzleHttp trying to use port 80 for https?

我正在尝试在 Laravel 5.3 应用程序中使用 GuzzleHttp 连接到 https://api.example.com(注意 https)。

<?php

$client = new GuzzleHttp\Client();

$res = $client->request('GET', 'https://api.example.com/accounts', [
    'headers' => [
        'Accept' => 'application/json',
        'Authorization' => "Token $token"
    ]
]);

var_dump($res->getBody());

但是,我收到一个错误,它无法连接到端口 80(!),尽管 url 中有 https。即使我指定端口 443(例如 https://api.example.com:443/accounts),它仍然给我这个关于端口 80 的错误:

cURL error 7: Failed to connect to api.example.com port 80:
Connection refused (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

为什么不能连接到端口 443?为什么它试图连接端口 80?

您是否错过了标记变量的 $(和双引号)?

也许你的意思是 'Authorization' => "Token {$token}" 可能是由于错误的身份验证而重定向 headers

在这里回答我自己的问题。我对此进行了更多调查,发现在请求 https://api.example.com/accounts

时我得到了 301 Moved Permanently / Location: http://api.example.com/accounts

我对为什么我会从 https 重定向到 http 感到困惑,但发现问题出在 api。原来你 必须 在末尾添加一个斜线。 https://api.example.com/accounts/ 不重定向到端口 80 但有效。