Last active
February 22, 2023 14:03
-
-
Save diloabininyeri/6efa33c5acbc2c3ac88251dca1acca00 to your computer and use it in GitHub Desktop.
an example of how we can create a concurrent http pool using multi curl in php...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Response | |
{ | |
public function __construct(private string $body, private CurlHandle $curlHandle) | |
{ | |
} | |
public function body(): string | |
{ | |
return $this->body; | |
} | |
public function ok(): bool | |
{ | |
return 200 === $this->status(); | |
} | |
public function status(): int | |
{ | |
return curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE); | |
} | |
public function __toString(): string | |
{ | |
return $this->body; | |
} | |
} | |
/** | |
* @author Dılo sürücü | |
*/ | |
class Http | |
{ | |
private CurlHandle $curlHandle; | |
public function __construct() | |
{ | |
$this->curlHandle = curl_init(); | |
} | |
/** | |
* @return CurlHandle | |
*/ | |
public function getCurlHandle(): CurlHandle | |
{ | |
return $this->curlHandle; | |
} | |
public function get(): self | |
{ | |
curl_setopt($this->curlHandle, CURLOPT_HTTPGET, 1); | |
return $this; | |
} | |
public function put(array $payload = []): self | |
{ | |
curl_setopt($this->curlHandle, CURLOPT_PUT, 1); | |
curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, $payload); | |
return $this; | |
} | |
public function post(array $payload = []): self | |
{ | |
curl_setopt($this->curlHandle, CURLOPT_POST, 1); | |
curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, $payload); | |
return $this; | |
} | |
public function url(string $url): self | |
{ | |
curl_setopt($this->curlHandle, CURLOPT_URL, $url); | |
curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true); | |
return $this; | |
} | |
} | |
class Pool | |
{ | |
private CurlMultiHandle $curlMultiHandle; | |
/** | |
* @var array<string,Http> | |
*/ | |
private array $aliases = []; | |
public function __construct() | |
{ | |
$this->curlMultiHandle = curl_multi_init(); | |
} | |
public function as(string $alias): Http | |
{ | |
return $this->aliases[$alias] = new Http(); | |
} | |
/** | |
* @return array<Response<>> | |
*/ | |
public function getResponses(): array | |
{ | |
$this->bindHttp(); | |
$this->execAllCurls(); | |
$responses = []; | |
foreach ($this->aliases as $as => $http) { | |
$responses[$as] = new Response( | |
curl_multi_getcontent($http->getCurlHandle()), | |
$http->getCurlHandle() | |
); | |
} | |
return $responses; | |
} | |
/*** | |
* @return void | |
*/ | |
private function execAllCurls(): void | |
{ | |
do { | |
curl_multi_exec($this->curlMultiHandle, $still_running); | |
} while ($still_running); | |
} | |
private function bindHttp(): void | |
{ | |
foreach ($this->aliases as $http) { | |
curl_multi_add_handle($this->curlMultiHandle, $http->getCurlHandle()); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
the using