Skip to content

Instantly share code, notes, and snippets.

@alfchee
Created October 7, 2015 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfchee/cefeace9a24d7157ff2f to your computer and use it in GitHub Desktop.
Save alfchee/cefeace9a24d7157ff2f to your computer and use it in GitHub Desktop.
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