BaiduUtils::getCurrentUrl PHP Method

getCurrentUrl() public static method

Get the url of current page.
public static getCurrentUrl ( ) : string
return string
    public static function getCurrentUrl()
    {
        $protocol = 'http://';
        if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
            $protocol = strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) . '://';
        } elseif (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
            $protocol = 'https://';
        }
        if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
            $host = $_SERVER['HTTP_X_FORWARDED_HOST'];
        } else {
            $host = $_SERVER['HTTP_HOST'];
        }
        $currentUrl = $protocol . $host . $_SERVER['REQUEST_URI'];
        $parts = parse_url($currentUrl);
        $query = '';
        if (!empty($parts['query'])) {
            // drop known oauth params
            $params = explode('&', $parts['query']);
            $retained_params = array();
            foreach ($params as $param) {
                if (self::shouldRetainParam($param)) {
                    $retained_params[] = $param;
                }
            }
            if (!empty($retained_params)) {
                $query = '?' . implode($retained_params, '&');
            }
        }
        // use port if non default
        $port = isset($parts['port']) && ($protocol === 'http://' && $parts['port'] !== 80 || $protocol === 'https://' && $parts['port'] !== 443) ? ':' . $parts['port'] : '';
        // rebuild
        return $protocol . $parts['host'] . $port . $parts['path'] . $query;
    }

Usage Example

Example #1
0
 /**
  * Get a Logout URL suitable for use with redirects.
  * 
  * @param string $accessToken Access token for current user
  * @param string $next Url to go to after a successful logout
  * @return String The URL for the logout flow
  */
 public function getLogoutUrl($accessToken, $next = '')
 {
     $params = array('access_token' => $accessToken, 'next' => $next ? $next : BaiduUtils::getCurrentUrl());
     return self::$BD_OAUTH2_ENDPOINTS['logout'] . '?' . http_build_query($params, '', '&');
 }