-
Notifications
You must be signed in to change notification settings - Fork 232
/
example_basic_auth.php
26 lines (20 loc) · 989 Bytes
/
example_basic_auth.php
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
<?php
use GuzzleHttp\Client;
use VDB\Spider\RequestHandler\GuzzleRequestHandler;
use VDB\Spider\Spider;
require_once __DIR__ . '/../vendor/autoload.php';
// Create Spider
$spider = new Spider('http://httpbin.org/basic-auth/foo/bar');
$requestHandler = new GuzzleRequestHandler();
// Set a custom Guzzle client that does basic auth. See Guzzle docs on how to use other types of authentication.
$requestHandler->setClient(new Client(['auth' => ['foo', 'bar', 'basic'], 'http_errors' => false]));
$spider->getDownloader()->setRequestHandler($requestHandler);
// Execute crawl
$spider->crawl();
// Finally we could do some processing on the downloaded resources
// In this example, we will echo the title of all resources
echo "\n\nRESPONSE: ";
foreach ($spider->getDownloader()->getPersistenceHandler() as $resource) {
echo "\n" . $resource->getResponse()->getStatusCode() . ": " . $resource->getResponse()->getReasonPhrase();
echo "\n" . $resource->getResponse()->getBody();
}