Created
December 30, 2019 01:14
-
-
Save girorme/987da1031898dc13bb8da4b1cc4f25d6 to your computer and use it in GitHub Desktop.
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
<?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