Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Make sure that it is a POST request.
- if (strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0) {
- throw new Exception('Request method must be POST!');
- }
- // Make sure that the content type of the POST request has been set to application/json
- $contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
- if (stripos($contentType, 'application/json') === false) {
- throw new Exception('Content type must be: application/json; BUT contentType = '.$contentType);
- }
- // Receive the RAW post data.
- $content = trim(file_get_contents("php://input"));
- // Attempt to decode the incoming RAW post data from JSON.
- // { nick: "nick", email: "mail", scores: 123 }
- $decoded = json_decode($content, true);
- // If json_decode failed, the JSON is invalid.
- if (!is_array($decoded)) {
- throw new Exception('Received content contained invalid JSON!');
- }
- // test
- //print_r($decoded);
- // Process the JSON.
- $command = $decoded["command"];
- $cnt = $decoded["cnt"]; // records count
- if (strcasecmp($command, 'top') != 0) {
- throw new Exception('Incorrect command: '.$command);
- }
- // DB CONNECTION
- $username = "db_uname";
- $password = "db_pass";
- $hostname = "localhost";
- $dbname = "myDB";
- //connection to the database
- $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
- //echo "Connected to MySQL<br>";
- $selected = mysql_select_db("monax_gamesdata", $dbhandle) or die("Could not select examples");
- // execute the SQL query and return records
- $result = mysql_query("SELECT email, nick, scores FROM branddush_scores ORDER BY scores DESC");
- // fetch tha data from the database
- //echo "result: ".$result;
- $res = array();
- while ($row = mysql_fetch_array($result) and $cnt > 0) {
- //echo "email: ".$row{'id'}." Name:".$row{'model'}." ".$row{'year'}."<br>";
- $arr = array(
- 'email' => $row["email"],
- 'nick' => $row["nick"],
- 'scores' => $row["scores"]
- );
- $res[] = $arr;
- $cnt--;
- }
- //echo "done";
- echo json_encode($res);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement