-
-
Notifications
You must be signed in to change notification settings - Fork 315
/
Crawler.php
77 lines (63 loc) · 2.43 KB
/
Crawler.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
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
<?php
declare(strict_types = 1);
namespace Embed\Http;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriFactoryInterface;
use Psr\Http\Message\UriInterface;
class Crawler implements ClientInterface, RequestFactoryInterface, UriFactoryInterface
{
private RequestFactoryInterface $requestFactory;
private UriFactoryInterface $uriFactory;
private ClientInterface $client;
private array $defaultHeaders = [
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:73.0) Gecko/20100101 Firefox/73.0',
'Cache-Control' => 'max-age=0',
];
public function __construct(?ClientInterface $client = null, ?RequestFactoryInterface $requestFactory = null, ?UriFactoryInterface $uriFactory = null)
{
$this->client = $client ?: new CurlClient();
$this->requestFactory = $requestFactory ?: FactoryDiscovery::getRequestFactory();
$this->uriFactory = $uriFactory ?: FactoryDiscovery::getUriFactory();
}
public function addDefaultHeaders(array $headers): void
{
$this->defaultHeaders = $headers + $this->defaultHeaders;
}
/**
* @param UriInterface|string $uri The URI associated with the request.
*/
public function createRequest(string $method, $uri): RequestInterface
{
$request = $this->requestFactory->createRequest($method, $uri);
foreach ($this->defaultHeaders as $name => $value) {
$request = $request->withHeader($name, $value);
}
return $request;
}
public function createUri(string $uri = ''): UriInterface
{
return $this->uriFactory->createUri($uri);
}
public function sendRequest(RequestInterface $request): ResponseInterface
{
return $this->client->sendRequest($request);
}
public function sendRequests(RequestInterface ...$requests): array
{
if ($this->client instanceof CurlClient) {
return $this->client->sendRequests(...$requests);
}
return array_map(
fn ($request) => $this->client->sendRequest($request),
$requests
);
}
public function getResponseUri(ResponseInterface $response): ?UriInterface
{
$location = $response->getHeaderLine('Content-Location');
return $location ? $this->uriFactory->createUri($location) : null;
}
}