index.php 1.4 KB

123456789101112131415161718192021222324252627282930
  1. <?php
  2. require_once("../vendor/autoload.php");
  3. use MongoDB\Client;
  4. // Requires the MongoDB PHP Driver
  5. // https://www.mongodb.com/docs/drivers/php/
  6. $userInput = file_get_contents('php://input');
  7. $userInputData = json_decode($userInput, true);
  8. if ($userInputData !== null) {
  9. $client = new Client('mongodb://kpadmin:AnandAyyappan1981@127.0.0.1:27017/?authSource=kpmaster');
  10. $collection = $client->selectCollection('kpmaster', 'kpu');
  11. $cursor = $collection->aggregate([['$geoNear' => ['near' => ['type' => 'Point', 'coordinates' => [$userInputData["long"], $userInputData["lat"]]], 'spherical' => true, 'query' => ['srvs' => ['$all' => [$userInputData["service"]]]], 'distanceField' => 'Dis', 'maxDistance' => $userInputData["maxdistance"], 'includeLocs' => 'loc']], ['$project' => ['ID' => '$oid', 'Name' => '$fn', 'Distance' => '$Dis', 'Location' => '$loc']]]);
  12. $responseDataJson = iterator_to_array($cursor);
  13. $dCount = Count($responseDataJson);
  14. if ($dCount > 0) {
  15. $message['status'] = true;
  16. $message['count'] = $dCount;
  17. $message['data'] = $responseDataJson;
  18. } else {
  19. $message['status'] = true;
  20. $message['count'] = $dCount;
  21. $message['statusMessage'] = "No Records Found!";
  22. }
  23. } else {
  24. $message['status'] = false;
  25. $message['statusCode'] = "KP001";
  26. $message['statusMessage'] = "Data Not Received";
  27. }
  28. echo json_encode($message);