php kinohod php Импорт данных по API кинохода author Golodnyi ochen go

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* kinohod.php
*
* Импорт данных по API кинохода
*
* @author Golodnyi <ochen@golodnyi.ru>
* @Date : 12.12.14
* @Time : 9:14
* @version 1.0
*/
class kinohod
{
private $_cities = array();
private $_cinemas = array();
private $_allowCities = array('Москва');
private $_api = 'https://kinohod.ru/api/rest/partner/v1/{METHOD}?apikey=КЛЮЧ';
/**
* @param array $cinemas
*
* @throws Exception
*/
private function setCinemas($cinemas = array())
{
if ( empty($cinemas) || ! count($cinemas) )
{
$date = new DateTime();
$dates = array();
for ($i = 0; $i < 7; $i ++)
{
$dates[$i] = $date->format('dmY');
$date->modify('+1 days');
}
$cinemas = array();
foreach ($this->getCities() as $city)
{
$api_cinemas = json_decode($this->request(str_replace('{METHOD}', 'cinemas', $this->_api . '&city=' . $city['id'])));
if ( ! $api_cinemas )
{
throw new Exception('setCinemas: invalid data');
}
foreach ($api_cinemas as $cinema)
{
$cinemas[$city['id']][$cinema->id] = array(
'shortTitle' => $cinema->shortTitle,
'website' => $cinema->website,
'network' => $cinema->network,
'mall' => $cinema->mall,
'photo' => $cinema->photo,
'isSale' => $cinema->isSale,
'city' => $city,
'address' => $cinema->address,
'id' => $cinema->id,
'location' => $cinema->location,
'phones' => $cinema->phones,
'goodies' => $cinema->goodies,
'title' => $cinema->title,
);
foreach ($dates as $d)
{
$api_seances = json_decode($this->request(str_replace('{METHOD}', 'cinemas/' . $cinema->id . '/schedules', $this->_api . '&city=' . $city['id'] . '&date=' . $d)));
foreach ($api_seances as $seance)
{
$cinemas[$city['id']][$cinema->id]['movie'][$seance->movie->id] = array(
'ageRestriction' => $seance->movie->ageRestriction,
'originalTitle' => $seance->movie->originalTitle,
'grossRevenueRus' => $seance->movie->grossRevenueRus,
'rating' => $seance->movie->rating,
'genres' => $seance->movie->genres,
'trailers' => $seance->movie->trailers,
'countries' => $seance->movie->countries,
'poster' => $seance->movie->poster,
'images' => $seance->movie->images,
'duration' => $seance->movie->duration,
'premiereDateRussia' => $seance->movie->premiereDateRussia,
'title' => $seance->movie->title,
'schedules' => $seance->schedules
);
}
}
}
}
}
$this->_cinemas = $cinemas;
}
/**
* @return array
*/
public function getCinemas()
{
if ( empty($this->_cinemas) || ! count($this->_cinemas) )
{
$this->setCinemas();
}
return $this->_cinemas;
}
public function __construct()
{
$cinemas = $this->getCinemas();
}
/**
* @param array $cities array(array('name' => '', 'id' => '', 'tzname' => '', 'alias' => ''))
*
* @throws Exception
*/
private function setCities($cities = array())
{
if ( empty($cities) || ! count($cities) )
{
$cities = array();
$api_cities = json_decode($this->request(str_replace('{METHOD}', 'cities', $this->_api)));
if ( ! $api_cities )
{
throw new Exception('setCities: invalid data');
}
foreach ($api_cities as $city)
{
if ( in_array($city->name, $this->_allowCities) )
{
$cities[] = array(
'name' => $city->name,
'id' => $city->id,
'tzname' => $city->tzname,
'alias' => $city->alias
);
}
}
}
$this->_cities = $cities;
}
/**
* @return array
*/
public function getCities()
{
if ( empty($this->_cities) || ! count($this->_cities) )
{
$this->setCities();
}
return $this->_cities;
}
private function request($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36');
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
}