Skip to content

Instantly share code, notes, and snippets.

@samm-git
Created April 19, 2020 16:02
Show Gist options
  • Save samm-git/dc614493a5e2118ea8f7565e78305752 to your computer and use it in GitHub Desktop.
Save samm-git/dc614493a5e2118ea8f7565e78305752 to your computer and use it in GitHub Desktop.
Check account status from the T-mobile CZ prepaid
<?php
$creds = array(
'username'=>'[email protected]',
'password'=>'MyPassWord123'
);
$postvars = '';
foreach($creds as $key=>$value) {
$postvars .= $key . "=" . $value . "&";
}
// login and fetch auth related cookies
$ch = curl_init('https://www.t-mobile.cz/.gang/login');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: gPcookie=1; "));
// Ask for the callback.
curl_setopt($ch, CURLOPT_HEADERFUNCTION, "curlResponseHeaderCallback");
$cookies = Array();
$result = curl_exec($ch);
switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
case 302: // if user/password matches there is redirects to another page
break;
default:
die('FATAL: Unexpected HTTP code: '. $http_code. ", check username/password\n");
}
foreach($cookies as $v){
$tmp=explode('=',$v[1],2);
$cookie_arr[$tmp[0]]=$tmp[1];
}
curl_close($ch);
// validate that we got all required cookies
$fields_req=array("JSESSIONID","gftCookie","gTcookie","gAcookie","gScookie");
foreach ($fields_req as $key) {
if(!isset($cookie_arr[$key])) {
die("FATAL: Unable to get cookie: $key\n");
}
}
// cookies expected on every request
$theader=array("Cookie: AJAXIBLE_JAVASCRIPT_ENABLED=true; gPcookie=1; JSESSIONID=".
$cookie_arr["JSESSIONID"]."; gftCookie=".$cookie_arr["gftCookie"].
"; gTcookie=".$cookie_arr["gTcookie"]."; gAcookie=".$cookie_arr["gAcookie"].
"; gScookie=".$cookie_arr["gScookie"].";");
// getting lazy block id to fetch balance block
$ch = curl_init("https://www.t-mobile.cz/muj-t-mobile/-/module/myTariff?_rcorevccm_WAR_vcc_menu=mainmenu&_rcorevccm_WAR_vcc_menuCode=myTariff");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $theader);
$result = curl_exec($ch);
if(!preg_match('/ data-lazy-loading="{&quot;rk&quot;:&quot;(\d+)&quot;}" id="(\d+)"/', $result, $lazy_ids)){
die("FATAL: Unable to get lazy block ids\n");
}
curl_close($ch);
// getting actual balance frame
$ch = curl_init('https://www.t-mobile.cz/muj-t-mobile/-/module/myTariff?p_p_id=rcorevccm_WAR_vcc&p_p_lifecycle=0&p_p_state=exclusive&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1&_rcorevccm_WAR_vcc_moduleCode=myTariff&_rcorevccm_WAR_vcc_lazyLoading=true&_rcorevccm_WAR_vcc_componentIds='.$lazy_ids[1].'.'.$lazy_ids[2]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $theader);
$result = curl_exec($ch);
curl_close($ch);
// normalize
$result=preg_replace('/\s+/'," ",$result);
if(!preg_match('/Výše kreditu<\/div> <\/th> <td class="text-right"> <div class="text-xlarge"> <strong>([\d,]+)<\/strong>/', $result, $matches)){
die("FATAL: Unable to get balance\n");
}
$balance=(float)str_replace(",",".",$matches[1]);
printf("Balance: %01.2f CZK\n", $balance);
// callback to get cooikes
function curlResponseHeaderCallback($ch, $headerLine) {
global $cookies;
if (preg_match('/^Set-Cookie:\s*([^;]*)/mi', $headerLine, $cookie) == 1)
$cookies[] = $cookie;
return strlen($headerLine); // Needed by curl
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment