1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TreeHouse\IoBundle\Scrape\Crawler\Client; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface as GuzzleClientInterface; |
6
|
|
|
use GuzzleHttp\Exception\RequestException; |
7
|
|
|
use GuzzleHttp\RequestOptions; |
8
|
|
|
use GuzzleHttp\TransferStats; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Log\LoggerAwareInterface; |
11
|
|
|
use Psr\Log\LoggerAwareTrait; |
12
|
|
|
use TreeHouse\IoBundle\Scrape\Exception\CrawlException; |
13
|
|
|
|
14
|
|
|
class GuzzleClient implements ClientInterface, LoggerAwareInterface |
15
|
|
|
{ |
16
|
|
|
use LoggerAwareTrait; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var GuzzleClientInterface |
20
|
|
|
*/ |
21
|
|
|
protected $guzzle; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string[] |
25
|
|
|
*/ |
26
|
|
|
protected $effectiveUrls = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param GuzzleClientInterface $guzzle |
30
|
|
|
*/ |
31
|
2 |
|
public function __construct(GuzzleClientInterface $guzzle) |
32
|
|
|
{ |
33
|
2 |
|
$this->guzzle = $guzzle; |
34
|
2 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritdoc |
38
|
|
|
*/ |
39
|
2 |
|
public function fetch($uri, $userAgent = null) |
40
|
|
|
{ |
41
|
2 |
|
$this->setEffectiveUri($uri, $uri); |
42
|
|
|
|
43
|
|
|
try { |
44
|
2 |
|
$response = $this->guzzle->request('GET', $uri, $this->getRequestOptions($userAgent)); |
45
|
|
|
|
46
|
2 |
|
return $this->transformResponse($uri, $response); |
47
|
|
|
} catch (RequestException $e) { |
48
|
|
|
$this->logger->error( |
49
|
|
|
sprintf('Error crawling: %s', $e->getMessage()), |
50
|
|
|
['url' => (string) $e->getRequest()->getUri()] |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
if ($response = $e->getResponse()) { |
54
|
|
|
return $this->transformResponse($uri, $response); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
throw new CrawlException($uri, sprintf('Error crawling url: %s', $e->getMessage()), $e->getCode(), $e); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $uri |
63
|
|
|
* @param string $effectiveUri |
64
|
|
|
*/ |
65
|
2 |
|
public function setEffectiveUri($uri, $effectiveUri) |
66
|
|
|
{ |
67
|
2 |
|
$this->effectiveUrls[$uri] = $effectiveUri; |
68
|
2 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $userAgent |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
protected function getRequestOptions($userAgent = null) |
76
|
|
|
{ |
77
|
|
|
$options = [ |
78
|
2 |
|
RequestOptions::ON_STATS => function (TransferStats $stats) { |
79
|
|
|
$this->setEffectiveUri($stats->getRequest(), $stats->getEffectiveUri()); |
|
|
|
|
80
|
2 |
|
}, |
81
|
|
|
]; |
82
|
|
|
|
83
|
2 |
|
if ($userAgent) { |
|
|
|
|
84
|
2 |
|
$options[RequestOptions::HEADERS]['User-Agent'] = $userAgent; |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
return $options; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $uri |
92
|
|
|
* @param ResponseInterface $response |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
2 |
|
protected function transformResponse($uri, ResponseInterface $response) |
97
|
|
|
{ |
98
|
|
|
return [ |
99
|
2 |
|
isset($this->effectiveUrls[$uri]) ? $this->effectiveUrls[$uri] : $uri, |
100
|
2 |
|
$response, |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: