PHP code example of jessecascio / spider

1. Go to this page and download the library: Download jessecascio/spider library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jessecascio / spider example snippets




use Spider\Connection;
use Spider\Component;

// build an array of queries
$queries   = array();
$queries[] = "SELECT SLEEP(FLOOR(0 + (RAND() * 1)))";
$queries[] = "SELECT SLEEP(FLOOR(0 + (RAND() * 1)))";
$queries[] = "SELECT SLEEP(FLOOR(0 + (RAND() * 1)))";

// create a MySQL Connection
$connection = new Connection\MySQL($dbname, $usr, $pwd, $host, $port);

// execute
$web = new Component\Web($connection);
$web->queries($queries);
$web->crawl();

// proceed
$web->results();


use Spider\Component;

$config = new Component\Config();
$config->processes(5);
$config->memory(100); // MB
$config->trace('/path/to/log');

$web = new Component\Web($connection, $config);

use Spider\Component;

$config = new Component\Config();
$config->parse('/path/to/file.ini');

use Spider\Component;
use Spider\Storage;

$memcached = new Storage\Memcached($host, $port);

$config = new Component\Config();
$config->storage($memcached);

// single callback
$web->crawl(function($data) {
    // do work
    return $data;
});

// multiple callbacks
$queries = array();
$queries['users']  = "SELECT * FROM users";
$queries['orders'] = "SELECT * FROM orders"

$web->queries($queries);
$web->crawl([
    'users' => function ($data) {
        // do work on users data
        return $data;
    },
    'orders' => function ($data) {
        // do work on orders data
        return $data;
    }
])