1 | <?php |
||
20 | class DefaultClient implements ClientInterface |
||
21 | { |
||
22 | /** |
||
23 | * Default request options. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | private $options = array( |
||
28 | 'http' => array( |
||
29 | 'header' => array( |
||
30 | "Accept: application/json\r\n", |
||
31 | ), |
||
32 | ), |
||
33 | ); |
||
34 | |||
35 | /** |
||
36 | * Remote's server URI. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | private $baseUri; |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | public function __construct(array $config = array(), array $options = array()) |
||
46 | { |
||
47 | $this->options = $options; |
||
48 | $this->baseUri = $config['base_uri']; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | public function call( |
||
55 | $endpoint = '/', |
||
56 | array $arguments = array(), |
||
57 | array $options = array(), |
||
58 | $fullResponse = false |
||
59 | ) { |
||
60 | $context = stream_context_create($this->processOptions($options)); |
||
61 | $response = @file_get_contents( |
||
62 | $this->buildUrl($endpoint, $this->processParameters($arguments)), |
||
63 | false, |
||
64 | $context |
||
65 | ); |
||
66 | |||
67 | if ($response === false) { |
||
68 | throw new ClientException('Invalid response.'); |
||
69 | } |
||
70 | |||
71 | if ($fullResponse) { |
||
72 | return $this->decodeResponse($response); |
||
73 | } |
||
74 | |||
75 | return $response; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | public function saveFile($fromUrl, $filePath) |
||
82 | { |
||
83 | $opened = @fopen($fromUrl, 'r'); |
||
84 | if ($opened) { |
||
85 | $result = file_put_contents($filePath, $opened); |
||
86 | if ($result) { |
||
87 | return true; |
||
88 | } |
||
89 | } |
||
90 | |||
91 | return false; |
||
92 | } |
||
93 | |||
94 | private function getBaseUrl() |
||
98 | |||
99 | private function buildUrl($url, $params) |
||
100 | { |
||
101 | $url = sprintf( |
||
110 | |||
111 | private function processParameters($params) |
||
119 | |||
120 | private function processOptions($options) |
||
130 | |||
131 | private function decodeResponse($response) |
||
141 | } |
||
142 |