Skip to content

Instantly share code, notes, and snippets.

@diloabininyeri
Last active February 22, 2023 14:03
Show Gist options
  • Save diloabininyeri/6efa33c5acbc2c3ac88251dca1acca00 to your computer and use it in GitHub Desktop.
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...
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());
}
}
}
@diloabininyeri
Copy link
Author

the using

$pool = new Pool();

$pool
    ->as('google')
    ->url('https://www.google.com')
    ->post(['name' => 'Dılo sürücü']);

$pool
    ->as('youtube')
    ->url('https://www.youtube.com')
    ->get();
$pool
    ->as('linkedin')
    ->url('https://www.linkedin.com')
    ->put([]);
    
$responses = $pool->getResponses();

$linkedin = $responses['linkedin'];
$youtube = $responses['youtube'];

echo $linkedin; //can print directly response 

echo $linkedin->ok();
echo $linkedin->status();
echo $linkedin->body();


echo $youtube->ok();
echo $youtube->body();
echo $youtube->status();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment