1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| <?php
// Fonction MULTITHREADING - TRAITEMENT EN PARALLELE DE PLUSIEURS CRAWL A LA FOIS:
function curlMulti($urls) {
$mh = curl_multi_init(); // Initialisation de la multi session cURL
// For each of the URLs in array
foreach ($urls as $id => $d) {
$ch[$id] = curl_init(); // Initialisation de la session cURL
$url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
curl_setopt($ch[$id], CURLOPT_URL, $url);
curl_setopt($ch[$id], CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch[$id], CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch[$id], CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch[$id], CURLOPT_FOLLOWLOCATION, true);
curl_multi_add_handle($mh, $ch[$id]); // Ajout de sessions cURL à cURL multi session
}
$running = NULL; // Définit $running sur NULL
do {
curl_multi_exec($mh, $running); // Exécution de plusieurs sessions cURL en parallèle
} while ($running > 0); // Tant que $running est supérieur à zéro
// For each cURL session
foreach($ch as $id => $content) {
$results[$id] = curl_multi_getcontent($content); // Ajoute des résultats au tableau $results
curl_multi_remove_handle($mh, $content); // Supprimer cURL multi session
}
curl_multi_close($mh); // Fermeture de la multi session cURL
return $results; // Renvoie le tableau des résultats
}
function followLinks($url) {
global $alreadyCrawled;
global $crawling;
$parser = new DomDocumentParser($url);
$linkList = $parser->getLinks();
foreach($linkList as $link) {
$href = $link->getAttribute("href");
if(strpos($href, "#") !== false) {
continue;
}
else if(substr($href, 0, 11) == "javascript:") {
continue;
}
$href = createLink($href, $url);
if(!in_array($href, $alreadyCrawled)) {
$alreadyCrawled[] = $href;
$crawling[] = $href;
}
}
// Instanciation de la Librairie React-HTTP basée sur ReactPHP (https://github.com/reactphp/http) importée tout en haut du Crawler (ce même fichier) pour rendre Asynchrone le Crawl:
$reactLoop = React\EventLoop\Factory::create();
$reactClient = new Browser($reactLoop);
$crawling = array_values(array_unique($crawling)); // Suppression des doublons du tableau et réindexation
// Remove an item from the array after we have crawled it. This prevents infinitely crawling the same page.
// Appel de la fonction "curlMulti" et transmission d'un tableau d'URL pour faire plusieurs requêtes curl en même temps:
$crawling = curlMulti($crawling);
$freshCrawled = [];
// Follow each link in the crawling array.
foreach($crawling as $site) {
// Output the page title, descriptions, keywords, URL, Icon, Image, Video, Feeds, etc... This output is
// piped off to an external file using the command line.
// getDetails($site);
// On traite ici le Crawl de façon Asynchrone via la Librairie "React-HTTP" basée sur "ReactPHP":
$reactClient->get($site)
->then(function (\Psr\Http\Message\ResponseInterface $response) {
$freshCrawled[] = getDetails((string) $response->getBody());
}
);
// Remove an item from the array after we have crawled it. This prevents infinitely crawling the same page.
if (in_array($site, $freshCrawled)) {
$crawling = array_diff($crawling, array($site));
}
}
// Continue to follow each link in the crawling array.
foreach($crawling as $siteCrawledAgain) {
followLinks($siteCrawledAgain);
}
}
$startUrls = ['http://alibaba.com', 'http://lemonde.fr', 'http://amazon.com', 'http://baidu.com', 'http://yahoo.com'];
// Begin the crawling process by crawling the starting link first (POUR UN NON MULTI-THREADING).
// followLinks($startUrl);
foreach($startUrls as $site) {
$siteTrim = trim($site);
$url = url_parser($siteTrim);
followLinks($url); // On lance le Crawl effectif
}
?> |