Skip to content

Instantly share code, notes, and snippets.

@girorme
Created December 30, 2019 01:14
Show Gist options
  • Save girorme/987da1031898dc13bb8da4b1cc4f25d6 to your computer and use it in GitHub Desktop.
Save girorme/987da1031898dc13bb8da4b1cc4f25d6 to your computer and use it in GitHub Desktop.
<?php
class HttpGrabThreaded {
private array $urls;
private array $runtimes;
private array $futures;
public function __construct(array $urls)
{
$this->urls = $urls;
$this->runtimes = [];
$this->futures = [];
}
public function run()
{
foreach ($this->urls as $i => $url) {
$this->runtimes[$i] = new \parallel\Runtime();
$this->futures[$i] = $this->runtimes[$i]->run(function($url) {
$response = get_headers($url, 1)[0];
echo sprintf("%s: %s\n", $url, $response);
}, [$url]);
}
do {
echo "Processing...\r";
} while (count(array_filter($this->futures, function ($future) {
return $future->done() === false;
})) > 0);
}
}
$urls = ['https://google.com', 'https://twitter.com', 'https://codely.tv/'];
$httpGrab = new HttpGrabThreaded($urls);
$httpGrab->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment