2017-05-31 79 views
0

连接到网站与未知的方案我有没有指定的方案URL列表,例:通过狂饮

  • github.com(仅适用于https);
  • what.ever(只适用于http);
  • google.com(支持两种方案)。

我需要使用狂饮(V6)的根路径(/)的内容,但我不知道他们的计划:httphttps

我可以解决我的任务而不发出2个请求吗?

+0

只有当网站自动重定向您并且您将Guzzle配置为遵循重定向。 –

回答

0

Guzzle默认会遵循重定向,所以除非你有一个明确的https列表,否则我会在缺少http的前提下添加http,并允许网站在只接受https请求时重定向(这是他们应该做的) 。

<?php 

require 'vendor/autoload.php'; 

use GuzzleHttp\Client; 

$response = (new Client)->get('http://github.com/', ['debug' => true]); 

响应:

> 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 
0

一般 - 不,你不能没有两个请求解决问题(因为一个有可能是没有重定向)。

你可以用Guzzle做2个异步请求,那么你可能会花费同一时间,但有一个适当的通用解决方案。

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

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

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

这一切,现在你有(每个协议)两种反应,你让他们并行。