public function execute() { parent::execute(); // Make sure all required parameters are used $required_parameters = array('id'); foreach ($required_parameters as $parameter) { if (!array_key_exists($parameter, $_GET)) { echo json_encode(array('error' => 'Missing parameter: \'' . $parameter . '\'')); exit; } } // Initialize database try { $m = new \MongoClient('mongodb://' . MONGODB_USERNAME . ':' . MONGODB_PASSWORD . '@' . MONGODB_HOST . '/' . MONGODB_DATABASE); $db = $m->selectDB(MONGODB_DATABASE); } catch (MongoConnectionException $e) { echo json_encode(array('error' => 'Database connection failed, please try again later')); exit; } // Query database $result = $db->interactions->findOne(array('_id' => new MongoId($_GET['id']))); // Output JSON data if (is_null($result)) { echo json_encode(array('error' => 'Message doesn\'t exist')); } else { echo json_encode($result); } }
public function execute() { parent::execute(); $time = time(); $output = array('current_time' => array('unix' => $time, 'iso' => date('Y-m-d H:i:s', $time))); echo json_encode($output); }
public function execute() { parent::execute(); // Make sure all required parameters are used $required_parameters = array('sources', 'after', 'filters'); foreach ($required_parameters as $parameter) { if (!array_key_exists($parameter, $_GET)) { echo json_encode(array('error' => 'Missing parameter: \'' . $parameter . '\'')); exit; } } // Initialize database try { $m = new \MongoClient('mongodb://' . MONGODB_USERNAME . ':' . MONGODB_PASSWORD . '@' . MONGODB_HOST . '/' . MONGODB_DATABASE); $db = $m->selectDB(MONGODB_DATABASE); } catch (MongoConnectionException $e) { echo json_encode(array('error' => 'Database connection failed, please try again later')); exit; } // Sources $sources = $_GET['sources']; if ($sources == 'all') { $sources = 'twitter,facebook,googleplus'; } $sources = explode(',', $sources); $sources_exist = array(); foreach ($sources as $source) { $sources_exist[] = array('interaction.type' => $source); } // Filters $filters = explode(',', $_GET['filters']); foreach ($filters as &$filter) { $filter = (int) $filter; } $query = array('internal.filter_id' => array('$in' => $filters)); // Time $after = $_GET['after']; if (!is_numeric($after)) { $after = strtotime($after); } $after = new MongoDate($after); if (isset($_GET['before'])) { $before = $_GET['before']; if (!is_numeric($before)) { $before = strtotime($before); } $before = new MongoDate($before); $query['interaction.created_at'] = array('$gt' => $after, '$lt' => $before); } else { $query['interaction.created_at'] = array('$gt' => $after); } // Apply subfilter if (array_key_exists('subfilter', $_GET) && !empty($_GET['subfilter'])) { $parser = new Parser(); $result = $parser->parse($_GET['subfilter']); $subfilterQuery = $this->buildSubfilterQuery($result); $query['$and'] = array($subfilterQuery, array('$or' => $sources_exist)); } else { $query['$or'] = $sources_exist; } // Geo near if (array_key_exists('near', $_GET) && array_key_exists('distance', $_GET) && is_numeric($_GET['distance'])) { $near = explode(',', $_GET['near']); if (count($near) != 2) { echo json_encode(array('error' => '\'near\' location is invalid')); exit; } // Build geoNear command with the existing query $command = array('geoNear' => 'interactions', 'near' => array((double) $near[0], (double) $near[1]), 'spherical' => true, 'maxDistance' => $_GET['distance'] / $this->earthRadius, 'query' => $query, 'limit' => 500); // Run command and change output to be the same as usual MongoDB queries $results = $db->command($command); $results = $results['results']; foreach ($results as &$result) { $dis = $result['dis']; // Save distance $result = $result['obj']; // Set result to be the exact object $result['internal']['dis'] = $dis * $this->earthRadius; // Store the distance in miles inside the internal part of the output } } else { if (array_key_exists('near', $_GET) && array_key_exists('distance', $_GET)) { parse_str($_GET['distance'], $location); if (array_key_exists('county', $location) && array_key_exists('state', $location)) { $query['internal.location.county'] = $location['county']; $query['internal.location.state'] = $location['state']; } elseif (array_key_exists('state', $location)) { $query['internal.location.state'] = $location['state']; } } $results = $db->interactions->find($query)->sort(array('interaction.created_at' => 1))->limit(500); // Hard limit to 500 entries at the time } // Output JSON data echo '['; $i = 0; // Results returned via command is an array instead of a MongoCursor object and thus needs to use count(...) instead if (is_object($results)) { $results = iterator_to_array($results); } $resultsCount = count($results); foreach ($results as $result) { $this->convertDates($result); echo json_encode($result); // The last item should not be appended by a comma if ($i < $resultsCount - 1) { echo ', '; } $i++; } echo ']'; }