using drupal 7 functions in external files I feel that the above title isn't very well worded since I can't figure how to state it properly using few words. Anyway, what I'd like to do is this:

$.post("foo.php", data, function(msg) {
   //the js part is easy enough to handle
});


This is simple enough but how do I get foo.php to use Drupal 7's functions? While it would probably make my life much easier to use ordinary php code to do all my database queries, I'd like to stick with Drupal functions to play it safe.
Make a copy of the index.php file and rename it to foo.php. index.php ships by default with Drupal 7 and can be found in the root of the Drupal source.

Remove the last call to menu_execute_active_handler() from the new file. This is the router that calls an appropriate page handler based on the query string. You wouldn't need this.

This leaves:

define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
/* Stub */


As you can see, Drupal is now bootstrapped fully. This makes the API methods of Drupal core and all other enabled core and contributed modules available for use in this script from the fourth line.