http_build_url

(PECL pecl_http >= 0.21.0)

http_build_urlConstruir una URL

Descripción

string http_build_url ([ mixed $url [, mixed $parts [, int $flags = HTTP_URL_REPLACE [, array &$new_url ]]]] )

Construye una URL.

Los componentes de la segunda URL se combinarán con los de la primera, tal como se especifique en el parámetro flags.

Parámetros

url

(componente/s de) una URL, o bien en forma de string o bien en forma de array asociativo como en la respuesta de parse_url()

parts

lo mismo que el primer parámetro

flags

máscara "or" de bits de constantes HTTP_URL; por omisión, HTTP_URL_REPLACE

new_url

se rellena con los componentes de la URL generada, como en los valores devueltos por parse_url()

Valores devueltos

Devuelve la nueva URL en forma de string o FALSE en caso de error.

Ejemplos

Ejemplo #1 Ejemplo de http_build_url()

<?php
echo http_build_url("http://user@www.example.com/pub/index.php?a=b#files",
    array(
        
"scheme" => "ftp",
        
"host" => "ftp.example.com",
        
"path" => "files/current/",
        
"query" => "a=c"
    
),
    
HTTP_URL_STRIP_AUTH HTTP_URL_JOIN_PATH HTTP_URL_JOIN_QUERY HTTP_URL_STRIP_FRAGMENT
);
?>

El resultado del ejemplo sería:

ftp://ftp.example.com/pub/files/current/?a=c

Ver también

add a note add a note

User Contributed Notes 6 notes

up
6
anon at example dot com
8 months ago
If you need to access the url to the current script you should examine $_SERVER['REQUEST_URI'].

Using an undocumented feature that gives the same functionality as existing PHP library calls is reckless.
up
6
Ant P.
4 years ago
This function has a useful undocumented feature - the defaults are set in such a way that calling it with no parameters returns the full URL of the page being accessed.
up
1
randyg at ernieball dot com
2 years ago
Although I've never used this function, based on the documentation it seems that the above code should also include the following:

<?php
   
if ( ! is_array( $url ) ) // Added - Randy
    // Parse the original URL
       
$parse_url = parse_url($url);
       
// allow parts to be a url Added - Randy
   
if ( ! is_array( $parts ) )
       
$parts = parse_url( $parts );
?>
up
2
michael at REMOVE-THIS-PART dot muryn dot name
6 months ago
To those that want to use http_build_url() without parameters to get the canonical page URL, be aware that this function seem to consider the equivalent of SCRIPT_NAME for the path instead of REQUEST_URI.

For example, if /example serve info.php (you can do this with mod_rewrite in Apache for example), while accessing http://example.com/example?test=1 this function will return http://example.com/info.php?test=1 instead of the URL the user see.  This could lead to obvious problems if you build other URL from this.

So like another comment say, it is reckless to use that undocumented feature.  Instead here is what I suggest:

<?php
function get_request_url()
{
    return
get_request_scheme() . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}

function
get_request_scheme()
{
    return (isset(
$_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
}
?>
up
1
pasafama at gmail dot com
29 days ago
It seems to me that the return value must always have a protocol, a host and a path. If they are not provided in the input, default values are added.

From what I saw, the default value for the protocol is 'http://', for the host is the hostname (if running from cli) or the variable $_SERVER['HTTP_HOST'], for the path is the variable $_SERVER['SCRIPT_NAME']
up
-1
Yzmir Ramirez
1 year ago
So you used parse_url(...) and looking to put it back together and seeing if this function will do? 

You may have seen some glue* functions that will rebuild the url with the parts that you have.  Try this:

<?php

   $parts
= parse_url($uri);
  
/* do your magic here on $parts */
  
$uri = http_build_url('', $parts);
?>

Notice the first param is empty string.  It also works with NULL and array() as well.
To Top