Get Data Usage For Cricket Wireless
 Page :   [ 1 ]    [ 2 ]  

I used AT&T for quite some time but eventually got fed up with paying upwards of $100 per month for a pretty minimal cell phone plan.  I think it was something like 600 minutes, unlimited SMS and I was grandfathered in to their unlimited data.  The unlimited data was in actuality something like 5 GB of high speed then it was throttled down to dial-up speed (weak!).  

So I jumped ship and went with Cricket Wirless (there is some irony that Cricket is an MVNO that uses AT&T's network!) and now pay about $40 per month for unlimited talk and SMS and get 2.5 GB of data (throttled to dial-up after 2.5 GB).  Since my data is somewhat limited I found myself using a mobile app from Cricket to periodically check my data to make sure I wouldn't run out before the end of my billing cycle (or at least try not to run out!)

However, in my opinion, their app was slow and cumbersome and it took more time than I thought it should to just login and navigate to the section that told me how much data I had used.

I decided to make my own iOS app to fetch and display just the information I was interested in.  In order to keep things simple I wrote and deployed some straight forward PHP script on a server that would login to Cricket and fetch the data.  I then exposed this script via an API that i could access from the mobile app I made.

For simplicity I have stripped out the parts of the script that dealt with the signed API request for the purposes of presenting it here.

 

As you can see the app display was VERY Simple :)

 

 

Here is the class that does the heavy lifting of logging in and fetching data from Cricket.

 

<?php 

class Cricket{

	const TIMEOUT = 30;
	const USERAGENT = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)';
	
	private static $cookieFile;
	
	private $sessionToken = false;
	private $home;
	private $subscribers;
	
	private $billCycleDate = '';
	private $nextBillDueDays = '';

	public function __construct($user, $pass, $sessionToken){

		$this->sessionToken = $sessionToken;
	
		$this->home = false;	
		$this->subscribers = false;
		
		$home = $this->_doHome();
		
		if ($home){
			
			$this->home = $home;
			$this->_doSubscribers();
			
		}
		
	}
	
	
	public function __destruct(){

		if (file_exists(Cricket::$cookieFile)){
			@unlink(Cricket::$cookieFile);
		}

	}
	
	
	public function getHome(){
		return $this->home;
	}
	
	
	public function getSubscribers(){
		return $this->subscribers;
	}
	
	
	public function getBillCycleDate(){
		return $this->billCycleDate;
	}
	
	
	public function getNextBillDueDays(){
		return $this->nextBillDueDays;
	}
	
	
	private function _doSubscribers(){

		$subscribers = false;
		
		$home = $this->home;
		
		if (is_array($home)){
		
			if (isset($home['billCycleDate'])){
				$this->billCycleDate = $home['billCycleDate'];
				
			}
			if (isset($home['nextBillDueDays'])){
				$this->nextBillDueDays = $home['nextBillDueDays'];
			}
		
			if (isset($home['subscribers'])){
		
				$subscribers = $home['subscribers'];
		
				if (is_array($subscribers)){
		
					foreach ($subscribers as $skey => $subscriber){
		
						$subscriber['usage'] = $this->getUsage($subscriber['ctn']);
		
						$subscribers[$subscriber['ctn']] = $subscriber;
		
					}
						
				}
		
			}
		
		}
		
		if (is_array($subscribers)){
			if (count($subscribers)){
				$this->subscribers = $subscribers;
			}
		}
		
	}
	
	
	private function _doHome(){

		$home = false;
	
		if ($this->sessionToken){
	
			$params = array();
			$params['sessionToken'] = $this->sessionToken;
	
			$response = $this->makeRequest('https://apimyaio1404.aiowireless.com/rest/authentication/home', '', json_encode($params));
	
			$json = json_decode($response, 1);
	
			if (is_array($json)){
					
				if (isset($json['status'])){
	
					$status = $json['status'];
	
					if ($status == 'success'){
							
						$home = $json;
							
					}
	
				}
					
			}
	
		}
	
		return $home;
	
	}
	
	
	private function getUsage($phoneNumber){

		$usage = false;

		if ($this->sessionToken){
				
			$params = array();
			$params['sessionToken'] = $this->sessionToken;
			$params['ctn'] = $phoneNumber;
				
			$response = $this->makeRequest('https://apimyaio1404.aiowireless.com/rest/usage/summary', '', json_encode($params));
				
			$json = json_decode($response, 1);
				
			if (is_array($json)){
					
				if (isset($json['status'])){
						
					$status = $json['status'];
						
					if ($status == 'success'){
							
						$usage = $json;
							
					}
						
				}
					
			}
				
		}

		return $usage;

	}
	
	
	public static function getByLogin($username, $password){

		self::$cookieFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR .  'cricketCookies';		
		
		$sessionToken = self::login($username, $password);

		if($sessionToken !== false){
			return new Cricket($username, $password, $sessionToken);
		}else{
			return false;
		}
	}
	
	
	private static function login($username, $password){
	
		$params = array();
		$params['username'] = $username;
		$params['password'] = $password;
			
		$response = self::makeRequest('https://apimyaio1404.aiowireless.com/rest/authentication/login', '', json_encode($params));
			
		$json = json_decode($response, 1);
			
		if (is_array($json)){
				
			if (isset($json['status'])){
				if (isset($json['sessionToken'])){
					return $json['sessionToken'];
				}
			}
				
		}
	
		return false;
	
	}
	
	
	private static function makeRequest($url, $referer, $jsonString){
		
		$ch = curl_init();

		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_REFERER, $referer);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_USERAGENT, self::USERAGENT);
		curl_setopt($ch, CURLOPT_TIMEOUT, self::TIMEOUT);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonString);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: */*", "Content-Type: application/json; charset=utf-8", "Content-length: " . strlen($jsonString)));
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLINFO_HEADER_OUT, true);
		curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
		curl_setopt($ch, CURLOPT_COOKIEFILE, self::$cookieFile);
		curl_setopt($ch, CURLOPT_COOKIEJAR, self::$cookieFile);

		$response = curl_exec($ch);
		$info = curl_getinfo($ch);
		curl_close($ch);

		$arr = array();
		
		$arr['headers'] = explode("\r\n", substr($response, 0, $info['header_size']));
		$arr['content'] = substr($response, $info['header_size']);

		return $arr['content'];

	}
	

}

?>

 

 

 (Page 1 of 2)