-
-
Save avladev/d2e7d994976423ef3dde to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# Settings | |
// URL for all releases | |
$all_url = 'http://concordiarx.mediaroom.com/api/newsfeed_releases/list.php'; | |
// URL template for a release (later ID is appended to the URL) | |
$single_url = 'http://concordiarx.mediaroom.com/api/newsfeed_releases/get.php?id='; | |
// Enable cache for test purposes (files will be downloaded just once). Set to false in production | |
$cache = true; | |
// Cache directory | |
$cache_dir = 'cache/'; | |
// Output type: | |
// empty string -> echo to the browser | |
// 'path/filename.xml' -> save as file on server | |
$output = 'result.xml'; | |
// Pause time between requests (in seconds) | |
$pause_time = 1; | |
// Maximum run time | |
$time_limit = 60; | |
set_time_limit($time_limit); | |
# Logic | |
// Check if allow_url_fopen is enabled | |
if( !ini_get('allow_url_fopen') ){ | |
die("Please enable 'allow_url_fopen' option in your php.ini file."); | |
} | |
// Create cache directory if not exists | |
if( $cache && !is_dir($cache_dir) ){ | |
mkdir($cache_dir) or die('Cannot create cache dir.'); | |
} | |
// Cache file for all realeses feed | |
$cache_file = $cache_dir . 'all.xml'; | |
// Load the XML from cache or refresh from URL | |
if( !$cache or !file_exists($cache_file) ){ | |
$all = file_get_contents($all_url); | |
file_put_contents($cache_file, $all); | |
sleep($pause_time); | |
}else{ | |
$all = file_get_contents($cache_file); | |
} | |
// DOMDocument for reading all releases data | |
$all_dom = new DOMDocument(); | |
$all_dom->loadXML($all) or die('Error loading all releases feed.'); | |
// XPath instance for extracting data from all releases feed | |
$xpath = new DOMXPath($all_dom); | |
// DOMDocument for resulting feed | |
$result_dom = new DOMDocument('1.0', 'UTF-8'); | |
// Append <releases> element to XML | |
$result_releases = new DOMElement('releases'); | |
$result_dom->appendChild($result_releases); | |
// Append the meta info from all releases feed | |
$result_releases->appendChild(new DOMElement('matching_count', $xpath->query('//releases/matching_count')->item(0)->nodeValue)); | |
$result_releases->appendChild(new DOMElement('returned_count', $xpath->query('//releases/returned_count')->item(0)->nodeValue)); | |
$result_releases->appendChild(new DOMElement('latestModified', $xpath->query('//releases/latestModified')->item(0)->nodeValue)); | |
// Find all release IDs and fetch the result | |
foreach($xpath->query('//release/id') as $id){ | |
// ID of the release | |
$id = $id->nodeValue; | |
// URL of release feed | |
$url = $single_url . $id; | |
// Cache file for release feed | |
$cache_file = $cache_dir . $id . '.xml'; | |
// Load from cache or refresh from URL | |
if( !$cache or !file_exists($cache_file) ){ | |
$single = file_get_contents($single_url . $id); | |
file_put_contents($cache_file, $single); | |
sleep($pause_time); | |
}else{ | |
$single = file_get_contents($cache_file); | |
} | |
// DOMDocument for release feed | |
$single_dom = new DOMDocument(); | |
$single_dom->loadXML($single) or die('Error loading release from ' . $url); | |
// Get <release> tag | |
$target_node = $single_dom->getElementsByTagName('release')->item(0); | |
// Clone the <release> tag and it's childs and append to resulting feed | |
$result_releases->appendChild($result_dom->importNode($target_node, true)); | |
} | |
if( $output ){ | |
// Save as file on disk | |
$result_dom->save($output); | |
}else{ | |
// Output to the browser | |
header('Content-type: text/xml'); | |
header('Content-encoding: UTF-8'); | |
echo $result_dom->saveXML(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment