July 3rd, 2007 How I did the IconBuffet Stamps page
Everyone keeps asking how I did the IconBuffet Stamps page. Here is the code. It has a couple little extras in there because I was planning on adding a “total possible points” calculation, but it seemed useless because “possible points” requires as many diamond deliveries as you can, and let’s face it…those are hard to make.
function data_encode($data, $keyprefix = "", $keypostfix = "") { assert( is_array($data) ); $vars=null; foreach($data as $key=>$value) { if (is_array($value)) { $vars .= data_encode($value, $keyprefix.$key.$keypostfix.urlencode("["), urlencode("]")); } else { $vars .= $keyprefix.$key.$keypostfix."=".urlencode($value)."&"; } } return substr($vars, 0, -1); } if (isset($_REQUEST['username'], $_REQUEST['password'])) { $post_data = array('email'=>$_REQUEST['username'], 'password'=>$_REQUEST['password']); $ch = curl_init("https://www.iconbuffet.com/people;login"); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies/ib_{$_REQUEST['username']}.cookie"); curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies/ib_{$_REQUEST['username']}.cookie"); curl_setopt($ch, CURLOPT_POSTFIELDS, data_encode($post_data)); $home_page = curl_exec($ch); $regex = '|'; $regex .= '<p>You have collected\s*(\d+)\s*Free Deliveries?</p>.*'; //get number of received deliveries $regex .= '<h4 class="stamps">\s*(\d+)\s*Stamps?</h4>.*'; //get number of stamps in bank $regex .= '<h4 class="tokens">\s*(\d+)\s*Tokens?</h4>.*'; //get number of tokens in bank $regex .= '|Ums'; preg_match($regex, $home_page, $matches); $deliveries['received'] = $matches[1]; $stamps['in_bank'] = $matches[2]; $tokens['in_bank'] = $matches[3]; curl_setopt($ch, CURLOPT_URL, "https://www.iconbuffet.com/people/{$_REQUEST['username']};stats"); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: text/javascript, text/html, application/xml, text/xml, */*")); $stats = curl_exec($ch); $regex = '!'; $regex .= '(\d+)\s*</strong>\s*Bronze.*'; //get Bronze deliveries $regex .= '(\d+)\s*</strong>\s*Silver.*'; //get Silver deliveries $regex .= '(\d+)\s*</strong>\s*Gold.*'; //get Gold deliveries $regex .= '(\d+)\s*</strong>\s*Diamond.*'; //get Diamond deliveries $regex .= '(\d+)\s*</strong>\s*(?:Person|People) referred.*'; //get People referred $regex .= "(\d+)\s*</strong>\s*VIP's referred.*"; //get VIP's referred $regex .= '!Ums'; preg_match($regex, $stats, $matches); $deliveries['bronze'] = $matches[1]; $deliveries['silver'] = $matches[2]; $deliveries['gold'] = $matches[3]; $deliveries['diamond'] = $matches[4]; $referrals['regular'] = $matches[5]; $referrals['vip'] = $matches[6]; $stamps['used'] = 0; $stamps['used'] += $deliveries['bronze']; $stamps['used'] += $deliveries['silver']*3; $stamps['used'] += $deliveries['gold']*5; $stamps['used'] += $deliveries['diamond']*10; $stamps['total'] = array_sum($referrals)*15 + $deliveries['received']*5; echo '<pre>'; echo sprintf("%-9s{$stamps['total']}\r\n", 'Total:'); echo sprintf("%-9s{$stamps['used']}\r\n", 'Used:'); echo sprintf("%-9s{$stamps['in_bank']}\r\n", 'In Bank:'); $stamps['in_wild'] = $stamps['total']-$stamps['used']-$stamps['in_bank']; echo sprintf("%-9s{$stamps['in_wild']}\r\n", 'In Wild:'); echo '</pre>'; curl_close($ch); } else { ?> <form action="" method="post"> <fieldset> <legend>Login</legend> <div> <label for="username">Username:</label> <input type="text" id="username" name="username" /> </div> <div> <label for="password">Password:</label> <input type="password" id="password" name="password" /> </div> <div class="submit"> <input type="submit" id="submit" name="submit" value="Login »" /> </div> </fieldset> </form> <?php } ?>
cartuning Says:
Great Aaron! Thanks for the informations.
!
I have analysed dyars excel stamp calculator so far: http://www.iconbuffet.com/people/dyar/blogs/ib-points-and-stamp-calculator
Cheers and keep on coding
Greg Says:
Is this the code that is seen on the right side of the page/after login? (in the margin where it is displaying the stamps total, or is this a seperate page?)
nice work!
Aaron D. Campbell Says:
This is the exact code that runs the IB Stamps page here. You simply log in with your IconBuffet username (not E-Mail) and password.
Ben Says:
Shouldn’t it be
Deliver(y|ies), notDeliveries??Ben Says:
That should have been code.
Shouldn’t it be Deliver(y|ies), not Deliveries??
Aaron D. Campbell Says:
Yes it should.