app.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. error_reporting(-1);
  3. require_once __DIR__ . '/../vendor/silica/silica/src/Silica/Application.php' ;
  4. $app = new Silica\Application();
  5. $app
  6. ->share('pdo', function($app) {
  7. $pdo = new PDO('mysql::host=192.168.100.102;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', array(
  8. PDO::ATTR_PERSISTENT => true ,
  9. ) ) ;
  10. return $pdo ;
  11. })
  12. >get('/json', function() {
  13. echo json_encode(array("message" => "Hello World!"));
  14. })
  15. >get('/db', function() use ($app) {
  16. $query_count = 1;
  17. if (TRUE === isset($_GET['queries'])) {
  18. $query_count = $_GET['queries'];
  19. }
  20. // Create an array with the response string.
  21. $arr = array();
  22. $id = mt_rand(1, 10000);
  23. // Define query
  24. $statement = $app['pdo']->prepare('SELECT randomNumber FROM World WHERE id = :id');
  25. $statement->bindParam(':id', $id, PDO::PARAM_INT);
  26. // For each query, store the result set values in the response array
  27. while (0 < $query_count--) {
  28. $statement->execute();
  29. // Store result in array.
  30. $arr[] = array('id' => $id, 'randomNumber' => $statement->fetchColumn());
  31. $id = mt_rand(1, 10000);
  32. }
  33. // Use the PHP standard JSON encoder.
  34. // http://www.php.net/manual/en/function.json-encode.php
  35. echo json_encode($arr);
  36. })
  37. >run() ;