总结:
1.GUZZLE包最终还是用的curl的curl_exec和curl_multi_exec去请求,可以添加自定义配置,这些配置最终还是会对应到 curl_setopt_array($easy->handle, $conf); 设置
2.还有一个就是可以做一些请求前或者请求返回后的中间件。介入请求的过程中处理$stack = new HandlerStack(); push,handler进去
//使用默认配置去构建客户端
$client = new Client();
//设置CURL,使用去构建客户端
$client = new \GuzzleHttp\Client([
'curl' => [
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 1,
]
]);
//代理设置
$proxy = [
//http域名代理
'http' => config('app.fileserver_proxy') . ':' . config('app.fileserver_port'), // Use this proxy with "http"
//https域名代理
'https' => config('app.fileserver_proxy') . ':' . config('app.fileserver_port'), // Use this proxy with "https",
//不需要代理的域名
'no' => ''
];
$response = $client->request('GET','https://www.baidu.com', [
'proxy' => $proxy
]);
//异步请求,要用wait才能返回
$promise = $client->requestAsync('GET', 'https://www.baidu.com');
$promise = $promise->then(
function (ResponseInterface $res) {
dd($res->getBody());
echo $res->getStatusCode() . "\n";
},
function (RequestException $e) {
dd($e);
echo $e->getMessage() . "\n";
echo $e->getRequest()->getMethod();
}
)->wait();
dd($promise);
function add_response_header($header, $value)
{
return function (callable $handler) use ($header, $value) {
return function (
RequestInterface $request,
array $options
) use ($handler, $header, $value) {
$promise = $handler($request, $options)
return $promise->then(
function (ResponseInterface $response) use ($header, $value) {
return $response->withHeader($header, $value);
}
);
}
};
}
$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(add_response_header('X-Foo', 'bar'));
$client = new Client(['handler' => $stack]);
$conf[CURLOPT_READFUNCTION] = function ($ch, $fd, $length) use ($body) {
return $body->read($length);
};
读取本地文件句柄流,提供POST数据, 可以解决那些大文件上传的, 另外一种方案就是切割成多个小文件上传。
番外:
可以设置CURLOPT_READFUNCTION 和CURLOPT_READDATA选项来为POST提供数据,同时,不能再设置CURLOPT_POSTFIELDS选项。当使用callback函数来提供数据时,一定要使用大块数据传输编码(chunked transfer-encoding)或者用CURLOPT_POSTFIELDSIZE 或CURLOPT_POSTFIELDSIZE_LARGE选项设置数据大小。chunked transfer-encoding可以用CURLOPT_HTTPHEADER来设置字段。
private function applyBody(RequestInterface $request, array $options, array &$conf)
{
$size = $request->hasHeader('Content-Length')
? (int) $request->getHeaderLine('Content-Length')
: null;
// Send the body as a string if the size is less than 1MB OR if the
// [curl][body_as_string] request value is set.
if (($size !== null && $size < 1000000) ||
!empty($options['_body_as_string'])
) {
$conf[CURLOPT_POSTFIELDS] = (string) $request->getBody();
// Don't duplicate the Content-Length header
$this->removeHeader('Content-Length', $conf);
$this->removeHeader('Transfer-Encoding', $conf);
} else {
$conf[CURLOPT_UPLOAD] = true;
if ($size !== null) {
$conf[CURLOPT_INFILESIZE] = $size;
$this->removeHeader('Content-Length', $conf);
}
$body = $request->getBody();
if ($body->isSeekable()) {
$body->rewind();
}
$conf[CURLOPT_READFUNCTION] = function ($ch, $fd, $length) use ($body) {
return $body->read($length);
};
}
// If the Expect header is not present, prevent curl from adding it
if (!$request->hasHeader('Expect')) {
$conf[CURLOPT_HTTPHEADER][] = 'Expect:';
}