|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Ivory Http Adapter package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Ivory\HttpAdapter; |
|
13
|
|
|
|
|
14
|
|
|
use Ivory\HttpAdapter\Message\InternalRequestInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Requests http adapter. |
|
18
|
|
|
* |
|
19
|
|
|
* @author GeLo <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class RequestsHttpAdapter extends AbstractHttpAdapter |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var \Requests_Transport */ |
|
24
|
|
|
private $transport; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Creates a requests http adapter. |
|
28
|
|
|
* |
|
29
|
|
|
* @param \Requests_Transport|null $transport The transport. |
|
30
|
|
|
* @param \Ivory\HttpAdapter\ConfigurationInterface|null $configuration The configuration |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct(\Requests_Transport $transport = null, ConfigurationInterface $configuration = null) |
|
33
|
|
|
{ |
|
34
|
|
|
parent::__construct($configuration); |
|
35
|
|
|
|
|
36
|
|
|
$this->transport = $transport; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getName() |
|
43
|
|
|
{ |
|
44
|
|
|
return 'requests'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function sendInternalRequest(InternalRequestInterface $internalRequest) |
|
51
|
|
|
{ |
|
52
|
|
|
$options = array( |
|
53
|
|
|
'timeout' => $this->getConfiguration()->getTimeout(), |
|
54
|
|
|
'connect_timeout' => $this->getConfiguration()->getTimeout(), |
|
55
|
|
|
'protocol_version' => (float) $this->getConfiguration()->getProtocolVersion(), |
|
56
|
|
|
'follow_redirects' => 0, |
|
57
|
|
|
'data_format' => 'body', |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
if ($this->transport !== null) { |
|
61
|
|
|
$options['transport'] = $this->transport; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
try { |
|
65
|
|
|
$response = \Requests::request( |
|
66
|
|
|
$uri = (string) $internalRequest->getUri(), |
|
67
|
|
|
$this->prepareHeaders($internalRequest), |
|
68
|
|
|
$this->prepareBody($internalRequest), |
|
|
|
|
|
|
69
|
|
|
$internalRequest->getMethod(), |
|
70
|
|
|
$options |
|
71
|
|
|
); |
|
72
|
|
|
} catch (\Exception $e) { |
|
73
|
|
|
throw HttpAdapterException::cannotFetchUri($uri, $this->getName(), $e->getMessage()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $this->getConfiguration()->getMessageFactory()->createResponse( |
|
77
|
|
|
$response->status_code, |
|
78
|
|
|
sprintf('%.1f', $response->protocol_version), |
|
79
|
|
|
$response->headers->getAll(), |
|
80
|
|
|
$response->body |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
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: