Advertisement
Guest User

Untitled

a guest
Mar 12th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. <?php
  2.  
  3. // Make sure that it is a POST request.
  4. if (strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0) {
  5. throw new Exception('Request method must be POST!');
  6. }
  7.  
  8. // Make sure that the content type of the POST request has been set to application/json
  9. $contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
  10.  
  11. if (stripos($contentType, 'application/json') === false) {
  12. throw new Exception('Content type must be: application/json; BUT contentType = '.$contentType);
  13. }
  14.  
  15. // Receive the RAW post data.
  16. $content = trim(file_get_contents("php://input"));
  17.  
  18. // Attempt to decode the incoming RAW post data from JSON.
  19. // { nick: "nick", email: "mail", scores: 123 }
  20. $decoded = json_decode($content, true);
  21.  
  22. // If json_decode failed, the JSON is invalid.
  23. if (!is_array($decoded)) {
  24. throw new Exception('Received content contained invalid JSON!');
  25. }
  26.  
  27. // test
  28. //print_r($decoded);
  29.  
  30. // Process the JSON.
  31. $command = $decoded["command"];
  32. $cnt = $decoded["cnt"]; // records count
  33.  
  34. if (strcasecmp($command, 'top') != 0) {
  35. throw new Exception('Incorrect command: '.$command);
  36. }
  37.  
  38. // DB CONNECTION
  39.  
  40. $username = "db_uname";
  41. $password = "db_pass";
  42. $hostname = "localhost";
  43. $dbname = "myDB";
  44.  
  45. //connection to the database
  46. $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
  47. //echo "Connected to MySQL<br>";
  48. $selected = mysql_select_db("monax_gamesdata", $dbhandle) or die("Could not select examples");
  49.  
  50. // execute the SQL query and return records
  51. $result = mysql_query("SELECT email, nick, scores FROM branddush_scores ORDER BY scores DESC");
  52. // fetch tha data from the database
  53.  
  54. //echo "result: ".$result;
  55.  
  56. $res = array();
  57. while ($row = mysql_fetch_array($result) and $cnt > 0) {
  58. //echo "email: ".$row{'id'}." Name:".$row{'model'}." ".$row{'year'}."<br>";
  59. $arr = array(
  60. 'email' => $row["email"],
  61. 'nick' => $row["nick"],
  62. 'scores' => $row["scores"]
  63. );
  64. $res[] = $arr;
  65. $cnt--;
  66. }
  67.  
  68. //echo "done";
  69. echo json_encode($res);
  70.  
  71. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement