How to get the base url?
If this is a fully bootstrapped Drupal site, the superglobal $base_url, should get you what you want. See: https://api.drupal.org/api/drupal/developer!globals.php/global/base_url/7

However, these globals are not initialized until Drupal is fully bootstrapped.

If you need to sniff the base URL before Drupal is fully bootstrapped you can use the PHP $_SERVER superglobal.

For example:

function _get_base_url(){
  $protocol = ($_SERVER['HTTPS'] &amp;&amp; ($_SERVER['HTTPS'] != "off")) ? "https" : "http";
  return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}