Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. <?php
  2.  
  3. use \Psr\Http\Message\ServerRequestInterface as Request;
  4. use \Psr\Http\Message\ResponseInterface as Response;
  5.  
  6. require '/var/www/html/strona/vendor/autoload.php';
  7.  
  8. $config = [
  9. 'settings' => [
  10. 'displayErrorDetails' => true,
  11. ],
  12. ];
  13. $app = new \Slim\App($config);
  14.  
  15. $app->get('/{miasto}', function ($request, $response, array $args) {
  16. $miasto = $args['miasto'];
  17.  
  18. $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
  19. $query = new MongoDB\Driver\Query(array('miasto'=>$miasto));
  20. $cursor = $manager->executeQuery('baza.miasta', $query);
  21.  
  22. //print_r($cursor->toArray());
  23. $tab= json_encode($cursor->toArray());
  24.  
  25. preg_match_all('/".*?"|\'.*?\'/', $tab, $matches);
  26. print_r($matches);
  27.  
  28. });
  29.  
  30.  
  31. $app->get('/{miasto}/{warunki}', function ($request, $response, array $args) {
  32.  
  33. $miasto = $args['miasto'];
  34. $warunki = $args['warunki'];
  35.  
  36. $manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
  37. $query = new MongoDB\Driver\Query(array('miasto'=>$miasto));
  38. $cursor = $manager->executeQuery('baza.miasta', $query);
  39.  
  40.  
  41. $tab= json_encode($cursor->toArray());
  42. //echo $tab;
  43. preg_match_all('/".*?"|\'.*?\'/', $tab, $matches);
  44. if($warunki=="temp")
  45. print_r($matches[0][6]);
  46. elseif($warunki=="opady")
  47. print_r($matches[0][4]);
  48. else
  49. echo "blad";
  50.  
  51. });
  52.  
  53.  
  54.  
  55.  
  56. $app->post('/', function ($request, $response, array $args) {
  57.  
  58. if (($_SERVER['PHP_AUTH_USER']!='admin')&&($_SERVER['PHP_AUTH_PW']!='admin'))
  59. {
  60. header('WWW-Authenticate: Basic realm="My Realm"');
  61. header('HTTP/1.0 401 Unauthorized');
  62. echo 'brak dostepu';
  63. exit;
  64. }
  65.  
  66. $bulk = new MongoDB\Driver\BulkWrite;
  67. $obj = file_get_contents("php://input", "r");
  68. $tresc=json_decode($obj,true);
  69. $_id1 = $bulk->insert($tresc);
  70.  
  71. $manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
  72. $result = $manager->executeBulkWrite("baza.miasta", $bulk);
  73. /*
  74. $miasto = $args['miasto'];
  75. $opady = $args['opady'];
  76. $temp = $args['temp'];
  77. $document1 = array( "miasto" => "$miasto",
  78. "opady" => "$opady",
  79. "temperatura" => "$temp");
  80.  
  81. $bulk = new MongoDB\Driver\BulkWrite;
  82. $_id1 = $bulk->insert($document1);
  83.  
  84.  
  85. $result = $manager->executeBulkWrite('baza.miasta', $bulk);
  86. */
  87.  
  88. });
  89.  
  90. $app->put('/{miasto}', function ($request, $response, array $args) {
  91.  
  92. if (($_SERVER['PHP_AUTH_USER']!='admin')&&($_SERVER['PHP_AUTH_PW']!='admin'))
  93. {
  94. header('WWW-Authenticate: Basic realm="My Realm"');
  95. header('HTTP/1.0 401 Unauthorized');
  96. echo 'brak dostepu';
  97. exit;
  98. }
  99.  
  100. $miasto = $args['miasto'];
  101. $string=file_get_contents("php://input");
  102. $tresc=json_decode($string,true);
  103. $bulk = new MongoDB\Driver\BulkWrite;
  104.  
  105. $bulk->update(['miasto'=>$miasto],
  106. ['$set' => $tresc],
  107. ['multi' => true]);
  108. $manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
  109. $result = $manager->executeBulkWrite("baza.miasta", $bulk);
  110. echo 'put dziala';
  111. });
  112.  
  113. $app->delete('/{miasto}', function ($request, $response, array $args) {
  114.  
  115. if (($_SERVER['PHP_AUTH_USER']!='admin')&&($_SERVER['PHP_AUTH_PW']!='admin'))
  116. {
  117. header('WWW-Authenticate: Basic realm="My Realm"');
  118. header('HTTP/1.0 401 Unauthorized');
  119. echo 'brak dostepu';
  120. exit;
  121. }
  122.  
  123. $miasto = $args['miasto'];
  124. $bulk = new MongoDB\Driver\BulkWrite;
  125. $bulk->delete(['miasto' => $miasto]);
  126.  
  127. $manager = new MongoDB\Driver\Manager('mongodb://localhost:27017');
  128. $result = $manager->executeBulkWrite("baza.miasta", $bulk);
  129. echo 'pomyslnie usunieto';
  130. });
  131.  
  132.  
  133. $app->run();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement