1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: ignatenkov |
5
|
|
|
* Date: 20.08.15 |
6
|
|
|
* Time: 22:05 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace YaWeather { |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class YaWeather { |
13
|
|
|
|
14
|
|
|
private $_citiyId; |
15
|
|
|
private $_url; |
16
|
|
|
private $_error = []; |
17
|
|
|
private $_success = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Construct $id is cityId |
21
|
|
|
* @param int $id |
22
|
|
|
*/ |
23
|
|
|
public function __construct($id = 27643, $url = "https://export.yandex.ru/weather-ng/forecasts/"){ |
24
|
|
|
$this->_citiyId = $id; |
25
|
|
|
$this->_url = $url; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Function to load weather from web |
30
|
|
|
*/ |
31
|
|
|
public function load() { |
32
|
|
|
|
33
|
|
|
$url = $this->CreateUrl(); |
34
|
|
|
|
35
|
|
|
$userAgent = 'Googlebot/2.1 (+http://www.google.com/bot.html)'; |
36
|
|
|
|
37
|
|
|
$ch = curl_init($url); |
38
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
39
|
|
|
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); |
40
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); |
41
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
42
|
|
|
$output = curl_exec($ch); |
43
|
|
|
|
44
|
|
|
if(curl_errno($ch)) |
45
|
|
|
$this->_error[] = "Error load weather from web. curl_error output: " .curl_error($ch)."."; |
46
|
|
|
else |
47
|
|
|
$this->_success[] = "Load weather from web OK."; |
48
|
|
|
$this->saveToFile($output); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Save output to File |
53
|
|
|
* @param $output |
54
|
|
|
*/ |
55
|
|
|
private function saveToFile($output) { |
56
|
|
|
$fileName = $this->getFileName(); |
57
|
|
|
|
58
|
|
|
$fh = fopen($fileName, 'w'); |
59
|
|
|
|
60
|
|
|
if(fwrite($fh, $output)) |
61
|
|
|
$this->_success[] = "File success write."; |
62
|
|
|
else |
63
|
|
|
$this->_error[] = "Error file save."; |
64
|
|
|
fclose($fh); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get Error & Success status |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function logFormat() { |
72
|
|
|
$str = ""; |
73
|
|
|
|
74
|
|
|
if($this->_error) |
75
|
|
|
$str .= "Error: ".implode(" ", $this->_error)."<br>"; |
76
|
|
|
if($this->_success) |
77
|
|
|
$str .= "Success: ".implode(" ", $this->_success)."<br>"; |
78
|
|
|
|
79
|
|
|
return $str; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get errors |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
public function getError() { |
87
|
|
|
return $this->_error; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get success |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
|
public function getSuccess(){ |
95
|
|
|
return $this->_success; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return parse weather |
101
|
|
|
* @return string|Models\City |
102
|
|
|
*/ |
103
|
|
|
public function getResult() { |
104
|
|
|
|
105
|
|
|
$parse = new ParseXml($this->getFileName()); |
106
|
|
|
|
107
|
|
|
if($parse->parse()) |
108
|
|
|
return $parse->parse(); |
109
|
|
|
else |
110
|
|
|
return false; |
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* show page html.php |
116
|
|
|
* @param bool $cp1251 |
117
|
|
|
*/ |
118
|
|
|
public function show($cp1251 = false) { |
|
|
|
|
119
|
|
|
$file = "html.php"; |
120
|
|
|
if($cp1251) |
121
|
|
|
$file = "htmlcp1251.php"; |
122
|
|
|
|
123
|
|
|
$city = $this->getResult(); |
124
|
|
|
|
125
|
|
|
$path = str_replace($_SERVER['DOCUMENT_ROOT'], "", __DIR__)."./../css"; |
126
|
|
|
|
127
|
|
|
if($city != null) |
128
|
|
|
require_once(dirname(__DIR__).'/'.$file); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Return url to load xml |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
private function CreateUrl() { |
136
|
|
|
return $this->_url . $this->_citiyId .".xml"; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get path |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getFileName() { |
144
|
|
|
return dirname(__DIR__)."/city_" . $this->_citiyId . ".xml"; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} |
148
|
|
|
} |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: