Skip to content

Commit 278c9e7

Browse files
author
Jérôme Poskin
committed
Merge pull request #19 from gbirke/zip_download
Add support for ZIP download
2 parents 86f6d25 + 74dd750 commit 278c9e7

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

examples/index.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
$serverTime = $tvdb->getServerTime();
1616
// Search for a show
1717
$data = $tvdb->getSeries('Walking Dead');
18+
1819
// Use the first show found and get the S01E01 episode
1920
$episode = $tvdb->getEpisode($data[0]->id, 1, 1, 'en');
2021
var_dump($episode);
@@ -25,6 +26,8 @@
2526
*/
2627

2728
/*
28-
$shows = $tvdb->search('Walking Dead');
29-
$episodes = $tvdb->getEpisodes($shows->Series->id, 'fr');
30-
echo count($episodes);*/
29+
// Get full series and episode info
30+
$episodes = $tvdb->getSerieEpisodes(153021, 'fr', Client::FORMAT_ZIP);
31+
var_dump($episodes["episodes"]);
32+
printf ("(%d Episodes)\n", count($episodes["episodes"]));
33+
*/

src/Moinax/TvDb/Client.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ public function getSerieEpisodes($serieId, $language = null, $format = self::FOR
245245
$data = $this->fetchXml('series/' . $serieId . '/all/' . $language . '.' . $format);
246246
break;
247247
case self::FORMAT_ZIP:
248+
if (!in_array('zip', stream_get_wrappers())) {
249+
throw new \ErrorException('Your PHP does nort support ZIP stream wrappers');
250+
}
251+
$data = $this->fetchZIP('series/' . $serieId . '/all/' . $language . '.' . $format, array(), self::GET, $language.".xml");
252+
break;
248253
default:
249254
throw new \ErrorException('Unsupported format');
250255
break;
@@ -351,6 +356,39 @@ protected function fetchXml($function, $params = array(), $method = self::GET)
351356
return $simpleXml;
352357
}
353358

359+
/**
360+
* Fetches data via curl and returns result
361+
*
362+
* @access protected
363+
* @param string $function The function used to fetch data in zip
364+
* @param array $params
365+
* @param string $method
366+
* @param string $file The file to extract from the ZIP
367+
* @return string The data
368+
*/
369+
protected function fetchZIP($function, $params = array(), $method = self::GET, $file=null)
370+
{
371+
if (strpos($function, '.php') > 0) { // no need of api key for php calls
372+
$url = $this->getMirror(self::MIRROR_TYPE_ZIP) . '/api/' . $function;
373+
} else {
374+
$url = $this->getMirror(self::MIRROR_TYPE_ZIP) . '/api/' . $this->apiKey . '/' . $function;
375+
}
376+
377+
$zipName = tempnam(sys_get_temp_dir(), "tvdb-");
378+
$zip = fopen($zipName, "w");
379+
fwrite($zip, $this->httpClient->fetch($url, $params, $method));
380+
fclose($zip);
381+
if (is_null($file)) {
382+
$file = $this->getDefaultLanguage().".xml";
383+
}
384+
$dataPath = "zip://".$zipName."#".$file;
385+
$data = file_get_contents($dataPath);
386+
387+
$simpleXml = $this->getXml($data);
388+
389+
return $simpleXml;
390+
}
391+
354392
/**
355393
* Fetch data with curl
356394
*
@@ -391,7 +429,7 @@ protected function getXml($data)
391429
throw new XmlException(implode("\n", $errors));
392430
}
393431
}
394-
throw new XmlException('Xml file cound not be loaded');
432+
throw new XmlException('Xml file could not be loaded');
395433
}
396434

397435
return $simpleXml;

0 commit comments

Comments
 (0)