Sample code for 30+ languages & platforms
PHP Requires Chilkat v9.5.0.62+

A Simple Web Crawler

See more Spider Examples

This demonstrates a very simple web crawler using the Chilkat Spider component.

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\Hashtable;
use Chilkat2\Spider;
use Chilkat2\StringBuilder;

$success = false;

$spider = new Spider();

// Domain membership and a bounded, last-in-first-out stack of seed URLs.
// Stack keys are consecutive integer positions represented as strings.
$seenDomains = new Hashtable();
$seedUrls = new Hashtable();
$pendingUrls = new Hashtable();
$stackKey = new StringBuilder();

// You will need to change the start URL to something else...
$seedUrls->AddStr('0', 'http://something.whateverYouWant.com/');
$pendingUrls->AddInt('http://something.whateverYouWant.com/', 1);

// Set outbound URL exclude patterns
// URLs matching any of these patterns will not be added to the
// collection of outbound links.
$spider->AddAvoidOutboundLinkPattern('*?id=*');
$spider->AddAvoidOutboundLinkPattern('*.mypages.*');
$spider->AddAvoidOutboundLinkPattern('*.personal.*');
$spider->AddAvoidOutboundLinkPattern('*.comcast.*');
$spider->AddAvoidOutboundLinkPattern('*.aol.*');
$spider->AddAvoidOutboundLinkPattern('*~*');

// Use a cache so we don't have to re-fetch URLs previously fetched.
$spider->CacheDir = 'c:/spiderCache/';
$spider->FetchFromCache = true;
$spider->UpdateCache = true;

while ($seedUrls->Count > 0) {
    // Remove the last pending URL from both the stack and its membership set.
    $stackKey->SetString('');
    $stackKey->AppendInt($seedUrls->Count - 1);
    $url = $seedUrls->LookupStr($stackKey->GetAsString());
    $seedUrls->Remove($stackKey->GetAsString());
    $pendingUrls->Remove($url);
    $spider->Initialize($url);

    // Spider 5 URLs of this domain.
    // but first, save the base domain in seenDomains
    $domain = $spider->GetUrlDomain($url);
    $seenDomains->AddInt($spider->GetBaseDomain($domain), 1);

    $numCrawled = 0;
    $success = true;
    while (($success) && ($numCrawled < 5)) {
        $success = $spider->CrawlNext();
        if ($success) {
            // Display the URL we just crawled.
            echo $spider->LastUrl, "\n";

            // If the last URL was retrieved from cache,
            // we won't wait.  Otherwise we'll wait 1 second
            // before fetching the next URL.
            if (!$spider->LastFromCache) {
                $spider->SleepMs(1000);
            }

            $numCrawled = $numCrawled + 1;
        }

        // If CrawlNext fails (no more URLs to crawl in this domain), success is false and the loop exits.
    }

    // Add the outbound links to seedUrls, except
    // for the domains we've already seen.
    for ($i = 0; $i < $spider->NumOutboundLinks; $i++) {
        $url = $spider->GetOutboundLink($i);
        $domain = $spider->GetUrlDomain($url);
        $baseDomain = $spider->GetBaseDomain($domain);
        if (!$seenDomains->Contains($baseDomain)) {
            // Don't let our list of seedUrls grow too large.
            if (($seedUrls->Count < 1000) && (!$pendingUrls->Contains($url))) {
                $stackKey->SetString('');
                $stackKey->AppendInt($seedUrls->Count);
                $seedUrls->AddStr($stackKey->GetAsString(), $url);
                $pendingUrls->AddInt($url, 1);
            }
        }
    }
}