Created
August 11, 2012 17:22
-
-
Save anthonysterling/3325848 to your computer and use it in GitHub Desktop.
PHP Class to lookup Virgin Media services status
This file contains hidden or 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 | |
class VirginMediaStatus | |
{ | |
const | |
SERVICE_ALL = 1, | |
SERVICE_BROADBAND = 2, | |
SERVICE_TV = 4, | |
SERVICE_PHONE = 8; | |
protected | |
$uri = 'https://my.virginmedia.com/faults/service-status', | |
$pattern = '~(?<=<span class=")(no-)?issues-found">([^<]+)(?=<)~'; | |
public function __construct() | |
{ | |
if( ! extension_loaded('curl')) | |
{ | |
throw new Exception('no curl'); | |
} | |
} | |
public function check($postcode) | |
{ | |
$html = $this->request($this->uri, array('postCode' => $postcode)); | |
$data = $this->parse($html); | |
$results = array( | |
self::SERVICE_BROADBAND => null, | |
self::SERVICE_TV => null, | |
self::SERVICE_PHONE => null | |
); | |
if($data) | |
{ | |
$results[self::SERVICE_BROADBAND] = $data[0]; | |
$results[self::SERVICE_TV] = $data[1]; | |
$results[self::SERVICE_PHONE] = $data[2]; | |
} | |
return $results; | |
} | |
protected function request($uri, array $data = array()) | |
{ | |
$handle = curl_init($uri); | |
curl_setopt_array($handle, array( | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_POST => true, | |
CURLOPT_POSTFIELDS => http_build_query($data) | |
)); | |
return curl_exec($handle); | |
} | |
protected function parse($html) | |
{ | |
$matches = array(); | |
preg_match_all($this->pattern, $html, $matches); | |
return isset($matches[2]) ? $matches[2] : false ; | |
} | |
} | |
$virgin = new VirginMediaStatus; | |
$status = $virgin->check('NE270QF'); | |
echo 'Broadband Status: ' . $status[VirginMediaStatus::SERVICE_BROADBAND] . PHP_EOL; | |
echo 'TV Status: ' . $status[VirginMediaStatus::SERVICE_TV] . PHP_EOL; | |
echo 'Phone Status: ' . $status[VirginMediaStatus::SERVICE_PHONE] . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment