Php 通过Guzzle连接到方案未知的站点

Php 通过Guzzle连接到方案未知的站点,php,curl,guzzle,guzzle6,guzzlehttp,Php,Curl,Guzzle,Guzzle6,Guzzlehttp,我有一个没有指定方案的URL列表,例如: github.com(仅适用于https) what.ever(仅适用于http) google.com(支持这两种方案) 我需要使用Guzzle(v6)获取其根路径(/)的内容,但我不知道它们的方案:http或https 我可以在不发出两个请求的情况下解决我的任务吗?Guzzle默认情况下会遵循重定向,因此除非您有一个明确的https URL列表,否则如果缺少http,我会预先添加http,如果网站只接受https请求(这是他们应该做的) 通常-不

我有一个没有指定方案的URL列表,例如:

  • github.com(仅适用于
    https
  • what.ever(仅适用于
    http
  • google.com(支持这两种方案)
我需要使用Guzzle(v6)获取其根路径(
/
)的内容,但我不知道它们的方案:
http
https


我可以在不发出两个请求的情况下解决我的任务吗?

Guzzle默认情况下会遵循重定向,因此除非您有一个明确的https URL列表,否则如果缺少http,我会预先添加http,如果网站只接受https请求(这是他们应该做的)


通常-不,没有两个请求就无法解决问题(因为其中一个请求可能没有重定向)

您可以使用Guzzle执行2个异步请求,然后您可能会花费相同的时间,但需要一个合适的通用解决方案

只需创建两个请求并等待两个请求:

 $httpResponsePromise = $client->getAsync('http://' . $url);
 $httpsResponsePromise = $client->getAsync('https://' . $url);

 list($httpResponse, $httpsResponse) = \GuzzleHttp\Promise\all(
     [$httpResponsePromise, $httpsResponsePromise]
 );

仅此而已,现在您有两个响应(针对每个协议),并且您可以并行地进行响应。

只有当站点自动重定向您并且您将Guzzle配置为跟随重定向时才可以。
> GET / HTTP/1.1
Host: github.com
User-Agent: GuzzleHttp/6.2.1 curl/7.51.0 PHP/5.6.30

< HTTP/1.1 301 Moved Permanently
< Content-length: 0
< Location: https://github.com/
< Connection: close
<
* Curl_http_done: called premature == 0
* Closing connection 0
*   Trying 192.30.253.112...
* TCP_NODELAY set
* Connected to github.com (192.30.253.112) port 443 (#1)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate: github.com
* Server certificate: DigiCert SHA2 Extended Validation Server CA
* Server certificate: DigiCert High Assurance EV Root CA
> GET / HTTP/1.1
Host: github.com
User-Agent: GuzzleHttp/6.2.1 curl/7.51.0 PHP/5.6.30

< HTTP/1.1 200 OK
< Server: GitHub.com
< Date: Wed, 31 May 2017 15:46:59 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Status: 200 OK
 $httpResponsePromise = $client->getAsync('http://' . $url);
 $httpsResponsePromise = $client->getAsync('https://' . $url);

 list($httpResponse, $httpsResponse) = \GuzzleHttp\Promise\all(
     [$httpResponsePromise, $httpsResponsePromise]
 );