Last active
October 13, 2015 08:58
-
-
Save ArnaudLigny/4171265 to your computer and use it in GitHub Desktop.
Print last version number and date of the DayZ Mod, as JSON format.
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 | |
$htmlPage = 'http://dayzmod.com/?Download'; // page to parse | |
$versionXPathQuery = '/html/body/section/div[1]/div/div[2]/div[1]/span[2]'; // version XPath query | |
$dateXPathQuery = '/html/body/section/div[1]/div/div[2]/div[2]/text()'; // date XPath query | |
$dateOriginYear = '2013'; // hard coded | |
try { | |
$date = null; | |
$version = null; | |
// HTTP request | |
$client = new Zend_Http_Client($htmlPage); | |
$response = $client->request(); | |
// Get/query DOM | |
$dom = new Zend_Dom_Query($response->getBody()); | |
// Search version | |
$versionResults = $dom->queryXpath($versionXPathQuery); | |
$count = count($versionResults); | |
if ($count >= 1) { | |
foreach ($versionResults as $result) { | |
$result = strip_tags(utf8_decode($result->nodeValue)); | |
$version = $result; | |
} | |
} | |
// Search date | |
$dateResults = $dom->queryXpath($dateXPathQuery); | |
$count = count($dateResults); | |
if ($count >= 1) { | |
foreach ($dateResults as $result) { | |
$result = strip_tags(utf8_decode($result->nodeValue)); | |
preg_match('`([[:alpha:]]*[ ]?[[:digit:]]{1,2}).*$`', $result, $matches); | |
$date_origin = $matches[1] . ", $dateOriginYear"; | |
$date = new Zend_Date($date_origin, Zend_Date::DATES, 'en_US'); | |
$date = $date->get('yyyy-MM-dd'); | |
} | |
} | |
// Prepare result array and JSON encode it | |
$result = array( | |
'date' => $date, | |
'version' => $version, | |
); | |
echo json_encode($result); | |
} catch (Zend_Http_Client_Adapter_Exception $e) { | |
echo json_encode(array('error' => $e->getMessage())); | |
} catch (Exception $e) { | |
echo json_encode(array('error' => $e->getMessage())); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment