PHP使用guzzlehttp实现request请求类
composer引入guzzlehttp
composer require guzzlehttp/guzzle
具体实现类
以下是使用php8.2版本的代码,低版本请取消类型化属性(7.4开始支持类型化属性), mixed类型需要php8以上,请确认版本后修改掉
<?php
namespace Test;//YOUR NAMESPACE
use GuzzleHttp\Pool;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
/**
* send requests
*
*/
class Requests
{
/**
* request domain
*
* @var string
*/
protected string $domain;
/**
* request uri
*
* @var string
*/
protected string $uri;
/**
* request method
*
* @var string
*/
protected string $method = 'POST';
/**
* default timeout
*
* @var int
*/
protected int $timeout = 5;
/**
* request headers
*
* @var array
*/
protected array $headers = [
'Content-Type' => 'application/json',
];
/**
* redirect param
* @var bool
*/
protected bool $redirect = false;
/**
* request data
* @var array
*/
protected array $data = [];
/**
* concurrency requests
*
* @param int $requestCount
* @param int $maxConcurrencyCount
*
* @return array
*/
final public function concurrency(int $requestCount = 10, int $maxConcurrencyCount = 5): array
{
$client = new Client([
'base_uri' => $this->domain,
'timeout' => $this->timeout,
]);
//request
$requests = function ($total) {
for ($i = 0; $i < $total; $i++) {
yield new Request($this->method, $this->uri);
}
};
if(!empty($this->headers)){
$this->data['headers'] = $this->headers;
}
$this->data['allow_redirects'] = $this->redirect;
$success = $error = [];
$pool = new Pool($client, $requests($requestCount), [
'concurrency' => $maxConcurrencyCount,
'options' => $this->data,
'fulfilled' => function ($response, $index) use (&$success) {
$success[$index] = $response->getBody()->getContents();
},
'rejected' => function ($reason, $index) use (&$error) {
if($reason instanceof RequestException){
$error[$index] = $reason->getResponse()->getBody()->getContents();
}else{
$error[$index] = $reason->getMessage();
}
},
]);
// Initiate the transfers and create a promise
$promise = $pool->promise();
// Force the pool of requests to complete.
$promise->wait();
return ['success' => $success, 'error' => $error];
}
/**
* send request
*
* @return string
* @throws GuzzleException
*/
final public function request(): string
{
$client = new Client([
'base_uri' => $this->domain,
'timeout' => $this->timeout,
]);
if(!empty($this->headers)){
$this->data['headers'] = $this->headers;
}
$this->data['allow_redirects'] = $this->redirect;
$response = $client->request($this->method, $this->uri, $this->data);
return $response->getBody()->getContents();
}
/**
* query request
*
* @param mixed $param
*
* @return $this
*/
final public function query(mixed $param): self
{
$this->data['query'] = $param;
return $this;
}
/**
* body request
*
* @param mixed $param
*
* @return $this
*/
final public function body(mixed $param): self
{
$this->data['body'] = $param;
return $this;
}
/**
* json request
*
* @param mixed $param
*
* @return $this
*/
final public function json(mixed $param): self
{
$this->data['json'] = $param;
return $this;
}
/**
* form request
*
* @param mixed $param
*
* @return $this
*/
final public function form(mixed $param): self
{
$this->data['form_params'] = $param;
return $this;
}
/**
* multipart request
*
* @param mixed $param
*
* @return $this
*/
final public function multipart(mixed $param): self
{
$this->data['multipart'] = $param;
return $this;
}
/**
* set domain
*
* @param string $domain
*
* @return $this
*/
final public function setDomain(string $domain): self
{
$this->domain = $domain;
return $this;
}
/**
* set uri
*
* @param string $uri
*
* @return $this
*/
final public function setUri(string $uri): self
{
$this->uri = $uri;
return $this;
}
/**
* set method
*
* @param string $method
*
* @return $this
*/
final public function setMethod(string $method): self
{
$this->method = $method;
return $this;
}
/**
* set timeout
*
* @param int $timeout
*
* @return $this
*/
final public function setTimeOut(int $timeout): self
{
$this->timeout = $timeout;
return $this;
}
/**
* set headers
*
* @param array $headers
*
* @return $this
*/
final public function setHeaders(array $headers): self
{
$this->headers = $headers;
return $this;
}
/**
* set redirect
*
* @param bool $redirect
*
* @return $this
*/
final public function setRedirect(bool $redirect): self
{
$this->redirect = $redirect;
return $this;
}
}
PHP使用guzzlehttp实现request请求类
https://lysowc.cn/archives/1719406666812