Created
December 6, 2011 01:20
-
-
Save liamzebedee/1436246 to your computer and use it in GitHub Desktop.
GoCard API - Liam Edwards-Playne
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
<? | |
/* | |
An Open-Source Unofficial API for the Translink GoCard System used in Queensland, Australia | |
Coded by Liam Edwards-Playne (liamzebedee) | |
Based on the works of Mike from CodeSmash Brisbane 2011 | |
Licensed under GPLv3 to Liam Edwards-Playne | |
*/ | |
function gocard_login($login_cardNumber, $login_password, $curlSession){ | |
curl_setopt($curlSession, CURLOPT_URL, 'https://www.seqits.com.au/webtix/welcome/welcome.do'); | |
curl_setopt($curlSession, CURLOPT_POSTFIELDS,'cardNum='.urlencode($login_cardNumber).'&pass='.urlencode($login_password).'&cardOps=Display'); | |
curl_setopt($curlSession, CURLOPT_POST, 1); | |
curl_setopt($curlSession, CURLOPT_HEADER, 0); | |
curl_setopt($curlSession, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($curlSession, CURLOPT_COOKIEJAR, "cookies.txt"); | |
curl_setopt($curlSession, CURLOPT_COOKIEFILE, "cookies.txt"); | |
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curlSession, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3"); | |
$page = curl_exec($curlSession); | |
if ( strstr( $page, "Please make sure the serial number and password are entered correctly." ) ){ | |
// Login error | |
return false; | |
} else { | |
// Login success | |
return true; | |
} | |
} | |
function gocard_get_historyBetweenDates($curlSession, $startDate, $endDate){ | |
$transaction_actionTypes = "/Top up|Touch on transfer |Touch on|Touch off|Auto top up/"; | |
/* | |
Returns a formatted array of GoCard history | |
$startDate and $endDate are to be formatted as unix timestamps | |
31-Jul-10 05:20:38 PM Top up 7 Eleven Manly West 10.00 | |
[day][0] => Day (1-31) | |
[month][1] => Month (3 Letter; Jan, Feb etc.) | |
[year][2] => Year (2 Digit; 10, 11 etc.) | |
[hour][3] => Hour (24 Hour Time) | |
[minute][4] => Minute (0 - 60; 03 - 3 Minutes) | |
[second][5] => Second (0 - 60; 03 - 3 Seconds) | |
[timeOfDay][6] => 12 Hour Clock Time (AM or PM) | |
[datetime][7] => Datetime in full | |
[action][8] => Action (Top up, Touch on, Touch on transfer, Touch off, Auto top up) | |
[place][9] => Place | |
[actionValue][10] => Action Value/Cost | |
*/ | |
$query_start_date = date("d-M-Y", $startDate); | |
$query_end_date = date("d-M-Y", $endDate); | |
curl_setopt($curlSession, CURLOPT_POST, 1); | |
curl_setopt($curlSession, CURLOPT_POSTFIELDS,'startDate='.urlencode($query_start_date).'&endDate='.urlencode($query_end_date)); | |
curl_setopt($curlSession, CURLOPT_URL, 'https://www.seqits.com.au/webtix/cardinfo/history.do'); | |
$pageData = curl_exec($curlSession); | |
$transactionRow_beginTag = "<tr class="; | |
$transactionRow_closeTag = "</tr>"; | |
preg_match_all("($transactionRow_beginTag.*$transactionRow_closeTag)siU", $pageData, $transactionTable); | |
$transactionRows = $transactionTable[0]; | |
$transaction_counter = 0; | |
$transactions = array(); | |
foreach($transactionRows as $transactionRowUnformatted){ | |
$transaction = gocard_util_parseTransactionXML($transactionTable[0][$transaction_counter]); | |
$day = substr($transaction,0,2); | |
$month = substr($transaction,3,3); | |
$year = substr($transaction,7,2); | |
$hour = substr($transaction,10,2); | |
$minute = substr($transaction,13,2); | |
$second = substr($transaction,16,2); | |
$timeOfDay = substr($transaction,19,2); | |
$datetime = substr($transaction,0,21); | |
$transaction_details = substr($transaction,22,strlen($transaction)-23); | |
$actionArray = preg_match($transaction_actionTypes, $transaction_details, $action); | |
$transaction_details_parsed = explode(" ",preg_replace("/$action[0]/","",$transaction_details)); | |
$actionValue = array_pop($transaction_details_parsed); | |
$place = implode(" ",$transaction_details_parsed); | |
$transactions[$transaction_counter] = array( | |
"day"=>$day, | |
"month"=>$month, | |
"year"=>$year, | |
"hour"=>$hour, | |
"minute"=>$minute, | |
"second"=>$second, | |
"timeOfDay"=>$timeOfDay, | |
"datetime"=>$datetime, | |
"action"=>$action[0], | |
"place"=>$place, | |
"actionValue"=>$actionValue | |
); | |
$transaction_counter++; | |
} | |
return $transactions; | |
} | |
function gocard_util_parseTransactionXML( $data ){ | |
$data = preg_replace('/ /',"",$data); | |
$xml = new SimpleXMLElement($data); | |
$currentArg = 0; | |
$dataString = ""; | |
foreach($xml->children() as $td){ | |
if($currentArg < 4){ | |
$cleanArg = trim(preg_replace('/\s+/'," ",preg_replace("/[\n\r]/","",(string)$td))); | |
$dataString .= $cleanArg.' '; | |
} | |
$currentArg++; | |
} | |
return $dataString; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment