1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Exchange Rate package, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* Implementation of exchange rate crawler for Banca Intesa Serbia, http://www.bancaintesa.rs. |
6
|
|
|
* |
7
|
|
|
* (c) 2017 RunOpenCode |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
namespace RunOpenCode\ExchangeRate\BancaIntesaSerbia\Util; |
13
|
|
|
|
14
|
|
|
use GuzzleHttp\Client; |
15
|
|
|
use GuzzleHttp\Cookie\CookieJar; |
16
|
|
|
use Psr\Http\Message\StreamInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class BancaIntesaBrowser |
20
|
|
|
* |
21
|
|
|
* Browser browses trough web site of Banca Intesa Serbia and fetches documents with rates. |
22
|
|
|
* |
23
|
|
|
* @package RunOpenCode\ExchangeRate\BancaIntesaSerbia\Util |
24
|
|
|
*/ |
25
|
|
|
class BancaIntesaBrowser |
26
|
|
|
{ |
27
|
|
|
const SOURCE = 'http://www.bancaintesa.rs/pocetna.21.html?print=1'; |
28
|
|
|
/** |
29
|
|
|
* @var Client |
30
|
|
|
*/ |
31
|
|
|
private $guzzleClient; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var CookieJar |
35
|
|
|
*/ |
36
|
|
|
private $guzzleCookieJar; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get HTML document with rates. |
40
|
|
|
* |
41
|
|
|
* @param \DateTime $date |
42
|
|
|
* @return StreamInterface |
43
|
|
|
*/ |
44
|
2 |
|
public function getHtmlDocument(\DateTime $date) |
45
|
|
|
{ |
46
|
2 |
|
return $this->request('POST', array(), array( |
47
|
2 |
|
'day' => $date->format('d'), |
48
|
2 |
|
'month' => $date->format('m'), |
49
|
2 |
|
'year' => $date->format('Y') |
50
|
|
|
)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Execute HTTP request and get raw body response. |
55
|
|
|
* |
56
|
|
|
* @param string $method HTTP Method. |
57
|
|
|
* @param array $params Params to send with request. |
58
|
|
|
* @return StreamInterface |
59
|
|
|
*/ |
60
|
2 |
|
private function request($method, array $query = array(), array $params = array()) |
61
|
|
|
{ |
62
|
2 |
|
$client = $this->getGuzzleClient(); |
63
|
|
|
|
64
|
2 |
|
$response = $client->request($method, self::SOURCE, array( |
65
|
2 |
|
'cookies' => $this->getGuzzleCookieJar(), |
66
|
2 |
|
'form_params' => $params, |
67
|
2 |
|
'query' => $query |
68
|
|
|
)); |
69
|
|
|
|
70
|
2 |
|
return $response->getBody()->getContents(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get Guzzle Client. |
75
|
|
|
* |
76
|
|
|
* @return Client |
77
|
|
|
*/ |
78
|
2 |
|
private function getGuzzleClient() |
79
|
|
|
{ |
80
|
2 |
|
if ($this->guzzleClient === null) { |
81
|
2 |
|
$this->guzzleClient = new Client(array('cookies' => true)); |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
return $this->guzzleClient; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get Guzzle CookieJar. |
89
|
|
|
* |
90
|
|
|
* @return CookieJar |
91
|
|
|
*/ |
92
|
2 |
|
private function getGuzzleCookieJar() |
93
|
|
|
{ |
94
|
2 |
|
if ($this->guzzleCookieJar === null) { |
95
|
2 |
|
$this->guzzleCookieJar = new CookieJar(); |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
return $this->guzzleCookieJar; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|