Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Chunks de código de intento de PHP asíncrono o con threadings para obtener contenido de un site
use GuzzleHttp\Client;
$request = new AsyncWebRequest($this->_url,$url);
if($request->start()) {
while($request->isRunning()) {
usleep(30);
}
if($request->join()) {
if($request->httpCode === 200) {
$links = $this->processLinks($request->content,$url,$depth);
foreach($links as $link) {
//$this->crawlPage($link,$depth -1);
if(!$this->isValid($link,$depth)) {
continue;
}
$this->_seen[$link] = true;
$rq = new AsyncWebRequest($this->_url,$link);
if($rq->start()) {
while($rq->isRunning()) {
usleep(30);
}
if($rq->join()) {
if($rq->httpCode === 200) {
$ls = $this->processLinks($rq->content,$link,$depth);
foreach($ls as $l) {
if(!$this->isValid($l,$depth)) {
continue;
}
$this->_seen[$l] = true;
}
}
}
}
}
}
}
}
class AsyncWebRequest extends Thread
{
public $baseUrl;
public $uri;
public $content = null;
public $httpCode = null;
public function __construct($baseUrl, $uri)
{
$this->baseUrl = $baseUrl;
$this->uri = $uri;
}//__construct()
public function run()
{
$client = new Client(array('base_uri' => $this->baseUrl));
try {
//$response = $client->request('GET', $this->uri, ['http_errors' => false]);
$response = file_get_contents($this->uri);
//var_dump($response);exit();
if($response) {
$this->content = $response;
$this->httpCode = 200;
} else {
$this->httpCode = 404;
}
} catch (RequestException $e) {
echo $e->getRequest();
if ($e->hasResponse()) {
echo $e->getResponse();
}
}
}//run()
}//WorkerThreads
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment