[php]
<?php
// Helper method to get a string description for an HTTP status code
// From http://www.gen-x-design.com/archives/create-a-rest-api-with-php/
function getStatusCodeMessage($status)
{
// these could be stored in a .ini file and loaded
// via parse\_ini\_file()... however, this will suffice
// for an example
$codes = Array(
100 =\> 'Continue',
101 =\> 'Switching Protocols',
200 =\> 'OK',
201 =\> 'Created',
202 =\> 'Accepted',
203 =\> 'Non-Authoritative Information',
204 =\> 'No Content',
205 =\> 'Reset Content',
206 =\> 'Partial Content',
300 =\> 'Multiple Choices',
301 =\> 'Moved Permanently',
302 =\> 'Found',
303 =\> 'See Other',
304 =\> 'Not Modified',
305 =\> 'Use Proxy',
306 =\> '(Unused)',
307 =\> 'Temporary Redirect',
400 =\> 'Bad Request',
401 =\> 'Unauthorized',
402 =\> 'Payment Required',
403 =\> 'Forbidden',
404 =\> 'Not Found',
405 =\> 'Method Not Allowed',
406 =\> 'Not Acceptable',
407 =\> 'Proxy Authentication Required',
408 =\> 'Request Timeout',
409 =\> 'Conflict',
410 =\> 'Gone',
411 =\> 'Length Required',
412 =\> 'Precondition Failed',
413 =\> 'Request Entity Too Large',
414 =\> 'Request-URI Too Long',
415 =\> 'Unsupported Media Type',
416 =\> 'Requested Range Not Satisfiable',
417 =\> 'Expectation Failed',
500 =\> 'Internal Server Error',
501 =\> 'Not Implemented',
502 =\> 'Bad Gateway',
503 =\> 'Service Unavailable',
504 =\> 'Gateway Timeout',
505 =\> 'HTTP Version Not Supported'
);
return (isset($codes[$status])) ? $codes[$status] : '';
}
// Helper method to send a HTTP response code/message
function sendResponse($status = 200, $body = '', $content\_type = 'text/html')
{
$status\_header = 'HTTP/1.1 ' . $status . ' ' . getStatusCodeMessage($status);
header($status\_header);
header('Content-type: ' . $content\_type);
echo $body;
}
class RedeemAPI {
private $db;
// Constructor - open DB connection
function \_\_construct() {
$this-\>db = new mysqli('localhost', '\*user', '\*password', '\*database');
$this-\>db-\>autocommit(FALSE);
}
// Destructor - close DB connection
function \_\_destruct() {
$this-\>db-\>close();
}
// Main method to redeem a code
function redeem() {
// Check for required parameters
if (isset($\_GET["rw\_app\_id"]) && isset($\_GET["code"]) && isset($\_GET["device\_id"])) {
// Put parameters into local variables
$rw\_app\_id = $\_GET["rw\_app\_id"];
$code = $\_GET["code"];
$device\_id = $\_GET["device\_id"];
// Look up code in database
$user\_id = 0;
$stmt = $this-\>db-\>prepare('SELECT id, unlock\_code, uses\_remaining FROM rw\_promo\_code WHERE rw\_app\_id=? AND code=?');
$stmt-\>bind\_param("is", $rw\_app\_id, $code);
$stmt-\>execute();
$stmt-\>bind\_result($id, $unlock\_code, $uses\_remaining);
while ($stmt-\>fetch()) {
break;
}
$stmt-\>close();
// Bail if code doesn't exist
if ($id \<= 0) {
sendResponse(400, 'Invalid code');
return false;
}
// Bail if code already used
if ($uses\_remaining \<= 0) {
sendResponse(403, 'Code already used');
return false;
}
// Check to see if this device already redeemed
$stmt = $this-\>db-\>prepare('SELECT id FROM rw\_promo\_code\_redeemed WHERE device\_id=? AND rw\_promo\_code\_id=?');
$stmt-\>bind\_param("si", $device\_id, $id);
$stmt-\>execute();
$stmt-\>bind\_result($redeemed\_id);
while ($stmt-\>fetch()) {
break;
}
$stmt-\>close();
// Bail if code already redeemed
if ($redeemed\_id \> 0) {
sendResponse(403, 'Code already used');
return false;
}
// Add tracking of redemption
$stmt = $this-\>db-\>prepare("INSERT INTO rw\_promo\_code\_redeemed (rw\_promo\_code\_id, device\_id) VALUES (?, ?)");
$stmt-\>bind\_param("is", $id, $device\_id);
$stmt-\>execute();
$stmt-\>close();
// Decrement use of code
$this-\>db-\>query("UPDATE rw\_promo\_code SET uses\_remaining=uses\_remaining-1 WHERE id=$id");
$this-\>db-\>commit();
// Return unlock code, encoded with JSON
$result = array(
"unlock\_code" =\> $unlock\_code,
);
sendResponse(200, json\_encode($result));
return true;
}
sendResponse(400, 'Invalid request');
return false;
}
}
// This is the first thing that gets called when this page is loaded
// Creates a new instance of the RedeemAPI class and calls the redeem method
$api = new RedeemAPI;
$api-\>redeem();
?\>
[/php]
Note: I removed user, password and database names from the above code [import]uid: 19620 topic\_id: 30174 reply\_id: 120882[/import]