PayPal\Auth\Oauth\OAuthUtil::get_headers PHP Method

get_headers() public static method

helper to try to sort out headers for people who aren't running apache
public static get_headers ( )
    public static function get_headers()
    {
        if (function_exists('apache_request_headers')) {
            // we need this to get the actual Authorization: header
            // because apache tends to tell us it doesn't exist
            $headers = apache_request_headers();
            // sanitize the output of apache_request_headers because
            // we always want the keys to be Cased-Like-This and arh()
            // returns the headers in the same case as they are in the
            // request
            $out = array();
            foreach ($headers as $key => $value) {
                $key = str_replace(" ", "-", ucwords(strtolower(str_replace("-", " ", $key))));
                $out[$key] = $value;
            }
        } else {
            // otherwise we don't have apache and are just going to have to hope
            // that $_SERVER actually contains what we need
            $out = array();
            if (isset($_SERVER['CONTENT_TYPE'])) {
                $out['Content-Type'] = $_SERVER['CONTENT_TYPE'];
            }
            if (isset($_ENV['CONTENT_TYPE'])) {
                $out['Content-Type'] = $_ENV['CONTENT_TYPE'];
            }
            foreach ($_SERVER as $key => $value) {
                if (substr($key, 0, 5) == "HTTP_") {
                    // this is chaos, basically it is just there to capitalize the first
                    // letter of every word that is not an initial HTTP and strip HTTP
                    // code from przemek
                    $key = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($key, 5)))));
                    $out[$key] = $value;
                }
            }
        }
        return $out;
    }

Usage Example

 /**
  * attempt to build up a request from what was passed to the server
  */
 public static function from_request($http_method = NULL, $http_url = NULL, $parameters = NULL)
 {
     $scheme = !isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on" ? 'http' : 'https';
     $http_url = $http_url ? $http_url : $scheme . '://' . $_SERVER['HTTP_HOST'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
     $http_method = $http_method ? $http_method : $_SERVER['REQUEST_METHOD'];
     // We weren't handed any parameters, so let's find the ones relevant to
     // this request.
     // If you run XML-RPC or similar you should use this to provide your own
     // parsed parameter-list
     if (!$parameters) {
         // Find request headers
         $request_headers = OAuthUtil::get_headers();
         // Parse the query-string to find GET parameters
         $parameters = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']);
         // It's a POST request of the proper content-type, so parse POST
         // parameters and add those overriding any duplicates from GET
         if ($http_method == "POST" && isset($request_headers['Content-Type']) && strstr($request_headers['Content-Type'], 'application/x-www-form-urlencoded')) {
             $post_data = OAuthUtil::parse_parameters(file_get_contents(self::$POST_INPUT));
             $parameters = array_merge($parameters, $post_data);
         }
         // We have a Authorization-header with OAuth data. Parse the header
         // and add those overriding any duplicates from GET or POST
         if (isset($request_headers['Authorization']) && substr($request_headers['Authorization'], 0, 6) == 'OAuth ') {
             $header_parameters = OAuthUtil::split_header($request_headers['Authorization']);
             $parameters = array_merge($parameters, $header_parameters);
         }
     }
     return new OAuthRequest($http_method, $http_url, $parameters);
 }