常见问题

Guzzle需要cURL吗?

不,Guzzle可以使用任何HTTP处理器来发送请求。这意味着,Guzzle 可以与cURL、PHP的流包装器、套接字和非阻塞库一起使用。 比如 React。你只需要配置一个HTTP处理器 来使用不同的方法来发送请求。

注意事项

Guzzle在历史上只使用cURL来发送HTTP请求。 是一个了不起的 HTTP 客户端(可以说是最好的),Guzzle 将继续使用 当它可用时,Guzzle将继续默认使用它。这很罕见,但有些开发者并没有 他们的系统中没有安装cURL,或者遇到了特定版本的问题。 通过允许可替换的HTTP处理程序,Guzzle现在变得更加可定制了 并且能够适应更多开发者的需求。

Guzzle可以发送异步请求吗?

是的,你可以使用requestAsync, sendAsync, getAsync headAsync, putAsync, postAsyncdeleteAsync, and patchAsync 的方法来发送一个异步请求。客户端将返回一个 GuzzleHttp\Promise\PromiseInterface 对象。你可以将then 函数来处理这个承诺。

$promise = $client->requestAsync('GET', 'http://httpbin.org/get');
$promise->then(function ($response) {
    echo 'Got a response! ' . $response->getStatusCode();
});

你可以使用返回的承诺的wait()方法强制完成一个异步响应。

$promise = $client->requestAsync('GET', 'http://httpbin.org/get');
$response = $promise->wait();

如何添加自定义cURL选项?

cURL提供了大量的可定制的选项。 虽然Guzzle在不同的处理程序中规范了许多这样的选项,但有时也需要设置自定义的cURL选项。 有些时候你需要设置自定义的cURL选项。这可以通过以下方式实现 通过在请求的curl键中传递一个cURL设置的关联数组来实现。 请求中的关联数组来实现。

例如,假设你需要定制一个客户使用的出站网络接口。

$client->request('GET', '/', [
    'curl' => [
        CURLOPT_INTERFACE => 'xxx.xxx.xxx.xxx'
    ]
]);

如果你使用cURL多处理程序的异步请求,并想对它进行调整。 额外的选项可以被指定为一个关联数组,在 CurlMultiHandler 构造函数的options键中指定。

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlMultiHandler;

$client = new Client(['handler' => HandlerStack::create(new CurlMultiHandler([
    'options' => [
        CURLMOPT_MAX_TOTAL_CONNECTIONS => 50,
        CURLMOPT_MAX_HOST_CONNECTIONS => 5,
    ]
]))]);

如何添加自定义的流媒体背景选项? ?

你可以通过自定义的 stream context options 使用请求选项的stream_context键。stream_context 是一个关联数组。 数组是一个关联数组,其中每个键是一个PHP传输,而每个值 是一个传输选项的关联数组。

例如,假设你需要定制一个客户使用的出站网络接口,并允许自签名的证书。

$client->request('GET', '/', [
    'stream' => true,
    'stream_context' => [
        'ssl' => [
            'allow_self_signed' => true
        ],
        'socket' => [
            'bindto' => 'xxx.xxx.xxx.xxx'
        ]
    ]
]);

为什么我得到一个SSL验证错误?

你需要在磁盘上指定Guzzle用于验证同伴证书的CA捆绑的路径。参见 验证

这个最大函数嵌套错误是什么?

Maximum function nesting level of '100' reached, aborting

如果你安装了XDebug扩展,并且在回调中执行了大量的请求,你可能会遇到这个错误。 你在回调中执行了大量的请求。这个错误信息是 特别是来自XDebug扩展。PHP本身并没有一个函数 嵌套限制。在你的php.ini中改变这个设置以增加限制。

xdebug.max_nesting_level = 1000

为什么我得到一个417的错误响应?

这可能是由于多种原因造成的,但如果你在发送PUT、POST、或 PATCH请求时,如果有一个Expect: 100-Continue头,不支持这个头的服务器 支持这个头的服务器将返回一个417的响应。你可以通过以下方式解决这个问题 将expect 请求选项设置为false

$client = new GuzzleHttp\Client();

// Disable the expect header on a single request
$response = $client->request('PUT', '/', ['expect' => false]);

// Disable the expect header on all client requests
$client = new GuzzleHttp\Client(['expect' => false]);

如何跟踪重定向请求?

你可以通过以下方式启用对重定向URI和状态代码的跟踪 <引用>track_redirects选项。每个被重定向的URI和状态代码都将被存储在 X-Guzzle-Redirect-HistoryX-Guzzle-Redirect-Status-History 头部。

初始请求的URI和最终状态代码将被排除在结果之外。考虑到这一点,你应该能够轻松地跟踪一个请求的完整重定向路径。

例如,假设你需要跟踪重定向,并在一份报告中同时提供两种结果。

// First you configure Guzzle with redirect tracking and make a request
$client = new Client([
    RequestOptions::ALLOW_REDIRECTS => [
        'max'             => 10,        // allow at most 10 redirects.
        'strict'          => true,      // use "strict" RFC compliant redirects.
        'referer'         => true,      // add a Referer header
        'track_redirects' => true,
    ],
]);
$initialRequest = '/redirect/3'; // Store the request URI for later use
$response = $client->request('GET', $initialRequest); // Make your request

// Retrieve both Redirect History headers
$redirectUriHistory = $response->getHeader('X-Guzzle-Redirect-History')[0]; // retrieve Redirect URI history
$redirectCodeHistory = $response->getHeader('X-Guzzle-Redirect-Status-History')[0]; // retrieve Redirect HTTP Status history

// Add the initial URI requested to the (beginning of) URI history
array_unshift($redirectUriHistory, $initialRequest);

// Add the final HTTP status code to the end of HTTP response history
array_push($redirectCodeHistory, $response->getStatusCode());

// (Optional) Combine the items of each array into a single result set
$fullRedirectReport = [];
foreach ($redirectUriHistory as $key => $value) {
    $fullRedirectReport[$key] = ['location' => $value, 'code' => $redirectCodeHistory[$key]];
}
echo json_encode($fullRedirectReport);