Skip to content

Instantly share code, notes, and snippets.

@inabhi9
Created July 9, 2014 12:53
Show Gist options
  • Save inabhi9/e7b4ae6ede42e133ca63 to your computer and use it in GitHub Desktop.
Save inabhi9/e7b4ae6ede42e133ca63 to your computer and use it in GitHub Desktop.
Get MTS 3g prepaid balance
<?php
function formatSizeUnits($bytes) {
if ($bytes >= 1073741824) {
$bytes = number_format($bytes / 1073741824, 2) . ' GB';
} elseif ($bytes >= 1048576) {
$bytes = number_format($bytes / 1048576, 2) . ' MB';
} elseif ($bytes >= 1024) {
$bytes = number_format($bytes / 1024, 2) . ' KB';
} elseif ($bytes > 1) {
$bytes = $bytes . ' bytes';
} elseif ($bytes == 1) {
$bytes = $bytes . ' byte';
} else {
$bytes = '0 bytes';
}
return $bytes;
}
// Getting from MTS
$mdn = $argv[1];
$cache_file = '/tmp/mtsbal.cache';
$data = "mdn=$mdn&billingsystem=TC&connectiontype=2";
$url = "https://selfcare.mtsindia.in/consumer/servicerequest/showBalanceInfoForm.action";
$tuCurl = curl_init();
curl_setopt($tuCurl, CURLOPT_URL, $url);
curl_setopt($tuCurl, CURLOPT_VERBOSE, 0);
curl_setopt($tuCurl, CURLOPT_HEADER, 0);
curl_setopt($tuCurl, CURLOPT_POST, 1);
curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $data);
$tuData = curl_exec($tuCurl);
curl_close($tuCurl);
// Getting data usage text
$pattern = "/Data usage available is[\s]+([0-9\.]+)/";
preg_match($pattern, $tuData, $matches);
$bal = -1;
if (count($matches) == 2){
$bal = formatSizeUnits($matches[1]*1024*1024);
file_put_contents($cache_file, $bal);
}
else{
if (file_exists($cache_file)){
$bal = file_get_contents($cache_file).'c';
}
}
echo $bal;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment